22 – Scanner class ( nextInt; hasNextInt ) | Java Tutorials

hasnext java
This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you 22 – Scanner class ( nextInt; hasNextInt ) | Java Tutorials. Following along are instructions in the video below:

continuing on with our scanner class lets now instead of just getting strings lets get some numbers so lets start with an integer example of that might be getting someones age so first lets ask for their age so that they know what to input and then lets create a input we learn normally that this would just get the next word they typed but I want to make sure that our return value is an integer so Im gonna type out next int this now will open up an input for us and whatever we type in it if its an integer it will return that value if its not an integer were going to get an error so running this it asks for our age we try to put a non integer value and hit enter we get this error of course but if we put the correct answer in which is any kind of integer which that is way too much and hit enter didnt notice it goes through perfectly so before we move on lets deal with the fact that we have a possibility of getting an error so lets make sure we dont get any errors while scanning this we can do this by using scan dot has next and has next well check to see if you actually have something to grab before you make a scan if theres nothing to scan next its gonna return false if you want to specify

this though we can do has next integer or like say you can do has next double if youre looking for a double and same down here if youre trying to get a double value you can do scan next double so scan has next int well open up an input for you to type in but it wont actually scan that value its just going to look at it and check to see if it is an integer or not and it will return true or false depending on what youve typed in that means we can use an if statement that said okay if we have an integer in our next scan then I want to save that integer in a scan of my own so we can just move this up here and afterwards I want to display out as well something like this now this is almost good but we have a problem here were trying to print out to the screen the a but our age variable is only going to get set up if we have an integer here if we dont this is going to be skipped altogether this whole if statement which means our age variable is never going to be set up thats why we get this error that says we cannot find the symbol and it says the variable age so to fix this you have to make sure that age is initialized regardless of what happens in

your code an easy solution would just be to put your print statement inside your if statement that way its only going to try to display this age variable if you actually have set it up first but if we want it to be outside of our if statement that means this age variable also has to be outside of our if statement while we have declared it we havent initialized it as it says here variable age might not have been initialized so this age variable still has nothing in it its a null value and we cant display that out a fix for this means that you have to have some kind of default value that age is going to resort to so setting H to 0 an even easier solution might just be to set H to 0 initially and then if the next thing you type is an integer then its going to change it from 0 to whatever you put so not the greatest solution but it still will get rid of that error and that way if they accidentally type something wrong its just going to default to be 0 instead of giving you an error what I like to do though is put the user in a loop so that they have to answer the question to move on so to do that lets create a while loop to put the person in while they keep typing in the wrong thing

so our condition here is going to check to see if they type in in in if they do type in an integer theres no need to put them in a loop right because they typed in the correct thing so were gonna skip it all together and then set up our value down below so I can get rid of this if statement right here so if they dont type in an integer then I want to go into the loop so were gonna do an exclamation point which reverses what this checks for so now this is checking for anything but in integer if they type in anything but an integer then Im put them in a loop here now remember hasnext creates an input for the person but it doesnt actually scan the item in it it looks at it and sees what it is and then returns true or false value but if you dont actually make a scan then its never going to move past that item so instead of trying to explain this Im just going to type this out so what this is going to do is if its not an integer then were just going to scan through it and then go back up around and check again if it is an integer then its going to skip this while loop and the next scan we make is now going to be actually saved whereas the scans and here are

used to get to the next item so now weve run this this has next integer opens up an input for us we type in an answer we hit enter on that that returns back true this exclamation makes that statement false so we skip over the wire loop and immediately scan that next item into this age if we run this again though and we try to put words in here and hit enter now notice it scans that next item throws it away and then loops back up and basically runs this condition again so now we can try again we hit Enter no go into Ana digit and as soon as we do it breaks out of our loop so this is working perfectly the last thing I would add is a print statement inside our while loop that basically ask the user to try again so Im gonna say only integers allowed and to try again now if we dont type in an integer and we try to hit enter its going to say only integers integers allowed and then to try again which of course once we do it well continue on with our code so that is how to deal with the primitive data types like say you can change this to double to get a double if you wanted to as well as you has next it can be changed to look for a double thanks for watching see you guys later

program, programming, code, coding, script, scripting, learn, how-to, beginner, free, start, basic, basics, hd, high, quality, voice, tutorial, tutorials, se…
Thank you for watching all the articles on the topic 22 – Scanner class ( nextInt; hasNextInt ) | Java Tutorials. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment