• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Scanner class: next() function

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that, when we give an input THEN the next() function is called,
OR the next() function is called first for us to give the input?
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well like other methods in java, next() will be called when the control reaches the line where it is invoked. input may or may not be present prior to invoking the next(). And as per docs, the call to this method may block for scanning the input. Generally for files, the method doesn't seems like blocking and in the case of standard input(from terminal) the scanner blocks the call waiting for the input.
 
Kunal Mukherjee
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, actually my confusion lies in the following piece of code:
   
   import java.util.Scanner;
   class A
   {
   public static void main(String args[])
   {
    Scanner sc=new Scanner(System.in);
   
    System.out.println("Enter the first number: ");
    int a=sc.nextInt();
    System.out.println("Enter the second number: ");
    int b=sc.nextInt();
     
    System.out.println(a);
    System.out.println(b);
    }
    }

When the first message "Enter the first number" is displayed, and then if I input both the values for a and b at once, on the same line
and then press ENTER, both are read. But after second message "
Enter the second number" is dispalyed, it does not take anymore input and directly prints the values of a and b,  although there is a call to the next() function after the second message.

This is what I can't get, why is the next()  function not called a second time?
 
Marshal
Posts: 69410
276
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please be specific when asking questions. You are not calling next() at all, but nextInt(), but don't worry; the explanation is the same for both.
You are printing a prompt and entering something at the terminal. Then you are calling nextInt(). Let's imagine that you are entering “123 456”. The input is only available to the terminal when you push the enter key. Right: that is two ints, and you call nextInt(), which reads 123. Then control passes to the second print instruction: that tells you to enter the second number. But you have already entered 456, so the second nextInt() call can run immediately; it already has its input 456. It is not a case of blocking waiting for the next input, but a sort of opposite. The input is already there, so the nextInt() call can run immediately. The same applies to all nextXXX() methods except nextLine() which behaves differently. Be sure to read the documentation comments before you use nextLine(); most of the books I have see explain nextLine() wrongly.
 
Campbell Ritchie
Marshal
Posts: 69410
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first nextInt() call only reads half the line you wrote; the remainder of the line is available immediately for the second nextInt() call.
 
Kunal Mukherjee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh now I get it, 😲  This made me keep scratching my head for two days, haha, now I'm clear about it. Thank you so much! 😃
 
Kunal Mukherjee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I have another doubt. Can you please answer my very first post?
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

User Kunal wrote:Oh I have another doubt. Can you please answer my very first post?


Campbell has all ready given very clear explanation for your 1st post which is indeed, the very first post of this thread. In case, if it's something else, go and start a separate thread. You should take a look at How to ask questions on Java Ranch, as the way you ask to answer the question is not a good example for asking questions here.
 
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your first question, scanner.nextXXX() methods are blocking, and they wait for user input.
 
Campbell Ritchie
Marshal
Posts: 69410
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Salil Wadnerkar wrote:. . . scanner.nextXXX() methods are blocking . . . wait for user input.

That isn't relevant here; there is no blocking nor waiting because the next input is already available.

PK: Thank you for finding my explanation clear.
 
Rancher
Posts: 4576
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Salil Wadnerkar wrote:. . . scanner.nextXXX() methods are blocking . . . wait for user input.

That isn't relevant here; there is no blocking nor waiting because the next input is already available.



I'd say that was relevant to the original question, which was asking, essentially, how nextXXX worked with input from the command line.  Does nextXXX get called after input arrives, or before.

The answer is, as Salil says, nextInt() gets called and will then wait for input to be available if it isn't available already.

In the actual example, this is what happens with the first call to nextInt, and for the second call there is no waiting as there is already a token in the buffer (as you say in your answer).

Essentially we have two, realted, questions here.
 
Campbell Ritchie
Marshal
Posts: 69410
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case, I am sorry for any mistakes.
 
I am mighty! And this is a mighty small ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic