laboon / cs0401

CS0401 Intermediate Java (Bill Laboon)
37 stars 75 forks source link

Question about Lab 3 b #119

Closed pavel121 closed 8 years ago

pavel121 commented 9 years ago

I have a problem to understand the last lab (lab 3 b). I did it, but I do not exactly understand how does it work. The first time that I ask a user to enter a number is here: if(input.hasNextInt())....... So, hasNextInt() method somehow asks the user to enter a number, but I don't understand why does it work. hasNextInt() returns true or false and just check if there is any integer. So why does it ask a user to enter a number? Does "hasNextInt()" is the same as "nextInt" and just return the same thing and do not go to the next token? Do both of them ask the user to enter something, if there is nothing next?

EthanWelsh commented 9 years ago

Pavel,

Scanner is definetly one of the more confusing parts about Java. Let me see if I can try to clear some of that up:

That last part about the buffer is a little weird, so let's look at an example:

Scanner s = new Scanner(System.in);
while(s.hasNext())
{
    System.out.println("INFINITY AND BEYOND");
}

The above loop will prompt you to enter in text once and then will proceed to print out "INFINITY AND BEYOND" forever. It will never prompt you for input again after your first time. This is because hasNext() initially asked you for input, but it never really 'took' that input from you: it never cleared its buffer. And therefore, because you never cleared Scanner's buffer, hasNext() will return true every time because it will still have your input just 'chilling' in its buffer. Contrast that example with this:

Scanner s = new Scanner(System.in);
while(s.hasNext())
{
    System.out.println("You said " + s.nextLine());
}

This program will stop ask you for input and will print out your input each time. It won't get into an infinite loop because you're always calling "nextLine()" after "hasNext()" asks you for input (clearing your buffer).

Hope this helps, E

laboon commented 9 years ago

Once again, @EthanWelsh with a great explanation. Pavel does this answer your question?

pavel121 commented 9 years ago

Yes, thank you for the answer. It is a good explanation. Of course, I need to read about this a bit more. But in general I understand how it works.

pavel121 commented 9 years ago

I understood how it works in this situation. But in a bit different situation it works differently. For example:

File file1 = new File("someTextFile.txt"); //If I understand it correctly, it provides a location of the txt file Scanner scanFile = new Scanner(file1);//If And this opens the file and put the data in a Scanner's buffer

while(scanFile.hasNextLine()){ //here I do not understand why eventually it returns false? System.out.println(scanFile.nextLine()); }

Here, eventually "scanFile.hasNextLine()" will return "false" after "scanFile.nextLine()" will clean the last line from Scanner's buffer. So the question is why "scanFile.hasNextLine()" returns false and does not just promote the user for a new input, like it does in the previous example, when the Scanner's buffer is empty. Because in the previous example, I do not see any option to get "false" if I use System.in, because it will just ask for additional input. Is it because of the difference in the Scanner's constructors, like some difference if Scanner's code?