Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: hasNextInt() question

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question hasNextInt() question

    Hello all,

    I'm relatively new to Java programming, I have a question about a piece of code from a video tutorial online:

    static Scanner userInput = new Scanner(System.in);
     
    	public static void main(String args[]){
     
    		System.out.print("Your favorite number: ");
     
     
    		if(userInput.hasNextInt()){
     
     
    			int numberEntered = userInput.nextInt();
     
    			System.out.println("You entered " + numberEntered);
    		}else{
    			System.out.println("Enter an integer next time.");
    		}
    }

    How is the if statement (userInput.hasNextInt()) "true" if it doesn't ask for user input before the if statement? The scanner asks for user input inside the if statement:

    int numberEntered = userInput.nextInt();

    It works for me, I just don't understand how. Thanks for any help in advance!!!


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: hasNextInt() question

    When you declare the Scanner variable, it automatically begins to read for user input. It will pause at userInput.hasNextInt until it reads input.
    So basically, user input is stored separately from the variables you declare. When you use .hasNextInt, it returns true or false, and when you use .nextInt, it returns the integer value itself.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: hasNextInt() question

    Take a look at this for more information on using the Scanner class.
    Scanner (Java 2 Platform SE 5.0)

    Also wrap your code in this format to preserve formatting.

    [C0DE=java]
    <code>
    [/C0DE]

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hasNextInt() question

    the scanner class as it is used here is for taking input from the standard input which is the keyboard. the hasNext() method of class Scanner is a method that returns true until the end of file indicator is pressed. the end of file indicator for windows is ctrl z while that of linux is ctrl d. so the application/program will continue to take input until you press the end of file indicator