d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question About This Code
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 27 2018 07:13pm
Code
package com.company;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter your year of birth:");
boolean hasNextInt = scanner.hasNextInt();


if(hasNextInt) {

int yearOfBirth = scanner.nextInt();
scanner.nextLine(); //Handles NewLine Key/character (Enter)

System.out.println("Enter your name: ");
String name = scanner.nextLine();
int age = 2018 - yearOfBirth;

if(age >= 0 && age <= 100) {
System.out.println("Your name is: " + name + " , and you are " + age + " years old.");
} else {
System.out.println("Invalid year of birth");
}

} else {
System.out.println("Unable to parse year of birth");
}


scanner.close();


}


}





I'm mostly confused about two parts.....
boolean hasNextInt = scanner.hasNextInt();
^Is that reading user input, and testing to see if it's an integer? if so, why do we see
int yearOfBirth = scanner.nextInt();
later? Isn't that redundant? Asking for user input of integer twice?
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Oct 28 2018 04:06am
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt()

Code
boolean hasNextInt()
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input.


This program checks if user entered integer as an input. If he/she did, yearOfBirth becomes initialized with that value.
Those lines aren't really redundant, although I haven't ever seen such a thing tbh. In a basic program like this it isn't necessary to check user input in that way.
Notice that scanner.hasNextInt() differs from scanner.nextInt() (link I mentioned about above). It doesn't read user input, but checks if it's integer.
Btw you may just test the program by running it...
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Oct 28 2018 10:14am
Please learn to read the documentation. These answers are all easily available by reading the very well written and concise documentation.
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextInt()

Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input.
Member
Posts: 7,206
Joined: May 11 2009
Gold: 5.00
Oct 29 2018 02:24pm
Usually you'll see this paradigm in a while loop... while(scanner.hasNext() { Object obj = scanner.getNext(); .... safe space to process } Thats a good signal that its doing what waraholic is talking about. IMO take tech documentation with a grain of salt, I've found nasty bugs in some of the top vendors 'bullet proof' libraries and frameworks. Might be worthwhile learning how to debug with a class compiler, then you will be L33T while all the documentation ppl taking hours to find the solution.
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Nov 5 2018 10:29pm
I'd have already read the documentation..... but luckily for me, someone i know explained it much better/more accurately then the official oracle documentation.
Anyways, thanks guys.
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Nov 6 2018 02:49pm
Quote (ferf @ Nov 6 2018 05:29am)
I'd have already read the documentation..... but luckily for me, someone i know explained it much better/more accurately then the official oracle documentation.
Anyways, thanks guys.


I see, but how any explanation could be better tho? :baby:
Oracle's one is excellent, perfectly noncomplicated and brief. Remember - documentation is your no. 1 friend in coding (ex aequo stackoverflow, probably ;) ).

This post was edited by pzold on Nov 6 2018 02:49pm
Member
Posts: 36,244
Joined: Nov 29 2005
Gold: Locked
Trader: Scammer
Jan 8 2019 12:00am
this is what java programming is. You have so many options and tricks! each lesson is heavy, man
the integer you entered in the command line is stored in memory by the inner methods of the Scanner class. It doesn't vanish once you make hasNextInt(), because you didn't input anything else, yet.

This post was edited by the_rest on Jan 8 2019 12:05am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll