File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question About Scanner Class and nextInt(), nextLine(), and next() Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question About Scanner Class and nextInt(), nextLine(), and next() Methods" Watch "Question About Scanner Class and nextInt(), nextLine(), and next() Methods" New topic
Author

Question About Scanner Class and nextInt(), nextLine(), and next() Methods

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I have distilled the following code from an exercise. I don't understand why nextLine() returns an empty String:



Here is the output:

Enter an int
50
Enter a sentence -- spaces are allowed
Enter a String -- with no spaces
15
i = 50
s1 =
s2 = 15

The program didn't block and wait for nextLine() to have data, it seems to have just returned "" immediately. Why would this be and what is the right way to fix this problem?

Edit: By the way, I'm running a Mac if that makes a difference. Does this have anything to do with line-endings? Do I need to set a pattern for line-endings in the Scanner object?
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Use scanner.nextLine(); after line 12.
It has got nothing to do with mac or windows.
When you use the same scanner object to take inputs int and then string then always use scannerobject.nextLine().
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Here is why you should use Scanner.nextLine() :

scanner.nextInt() will only take integer value entered and not the \n(When we hit enter after entering the integer). So this \n is still in the buffer, so when we use scanner.nextLine() after sacn.nextInt() it takes or stores \n.That's why nextLine() was returning empty line.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

OK Thanks.

I read that it isn't good to use nextInt() anyway because it doesn't validate. I believe that you can get stuck on nextInt() when it's in a loop and the next character is not a digit and it never will be.

Would you say that you should always use nextLine() or next() and always input String objects and then do validation in your own Java code?
Rohan Deshmkh
Ranch Hand

Joined: Aug 31, 2012
Posts: 127
Kaydell Leavitt wrote:
I read that it isn't good to use nextInt() anyway because it doesn't validate. I believe that you can get stuck on nextInt() when it's in a loop and the next character is not a digit and it never will be.

I haven't came across this, maybe you could give some example, then we can find it out.
Kaydell Leavitt wrote:
Would you say that you should always use nextLine() or next() and always input String objects and then do validation in your own Java code?


I don't think that we should always ignore nextInt() , it's there for a purpose.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Rohan Deshmkh wrote:
I don't think that we should always ignore nextInt() , it's there for a purpose.


I agree. If it were useless, it would be deprecated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Kaydell Leavitt wrote: . . . If it were useless, it would be deprecated.
You are a real incurable optimist there
You can get problems with nextLine() because you don’t know what it actually does. Look here, where somebody got confused about nextLine.
Search for a combination of Scanner nextInt and hasNextInt, and you should find some suggestions about how to prevent nextInt throwing an Exception when you pass input in the wrong format.

And happy joining anniversary
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question About Scanner Class and nextInt(), nextLine(), and next() Methods