aspose file tools*
The moose likes Beginning Java and the fly likes Problem with the Scanner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with the Scanner Class" Watch "Problem with the Scanner Class" New topic
Author

Problem with the Scanner Class

Andreas L. Berg
Greenhorn

Joined: Oct 10, 2007
Posts: 8
Hi there,

im pretty new in Java Programming (just started before 3 Weeks) and have now a Problem.

Id tried to write a little exercise program and get a curious issue.

here is the code:

(Its in German, sorry for it but i am German ;o) )

After the "eingabeNr" int i want to get via Scanner a String at "eingabe", but it wont work.
Someone told me that i should write it without Line, means in this case "eingabe = scan.next();"
Then it should work.
Every source i read tells me about the Scanner command, it should be written as i did, so im kinda confused about it.

Can someone tell me what the deal is about it? (Maybe in a really slow and easy way?)

Best Regards from Germany

Andy
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

Hi Andreas, welcome to JavaRanch! You don't have to apologize for being German. Millions of people are you know! I'm 37.5% German myself ... not that I can speak any.

The thing about the scanner is it doesn't get fed any data until Enter is pressed, and then it parses as much off that line as it can. In this case, say the user presses 1. Nothing happens. Then the user presses Enter. The scanner now has two characters to work with. The '1' and an invisible-to-you new line character. If you're running on Windows, then the scanner would also have an invisible carriage return character. So, it reads 1 as an integer, and the program goes on to print out your next prompt and then asks scanner to read a line. The scanner still has the new line and maybe a carriage return in its buffer, so it interprets that as an empty line and the program goes on. The solution is to add an extra "scanner.nextLine()" right before your second prompt, "Geben Sie den neuen Name ein: ". You don't even need to assign the result to anything. Just reading it will clear out that buffer, and make the scanner ready for the next input.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Greg Charles wrote:Hi Andreas, welcome to JavaRanch!
Agree
You don't have to apologize for being German. . . . .


That is a common problem, which is described poorly in many books; some books even say that nextLine() returns the next line. The official documentation says
the rest of the current line
…but that is not obvious until you have faced this problem. Another version of the same explanation here.
Andreas L. Berg
Greenhorn

Joined: Oct 10, 2007
Posts: 8
You don't have to apologize for being German. . . . .

😳…well it was more meant that i post here code that is spiked with another language then english, not especially that im german…… ;o)
But thanks for your regard ^^

Ok back to the topic,
i have to repeat what i understood just in case that i don´t get it right.
It is actually not a bug in Java or so, it is just the behavior of an old Typewriter. I have to write down before every new request scanner.nextLine(); so that the slade quasi gets back to the begin of a new line. (so actually the behavior of the "return" key, or?)
Isn´t there a command that i can set after the first scanner, that sets the slade automatically back?
Something like scan.nextLine(){return}; (im sure its wrong written ;o) but you guys know what i mean though)

I don´t know but im kinda sure, that im not the only one with that issue, so there must be a nice and elegant solution for it, or is the "scanner.nextLine();" already the best way?


Best Regards

Andy
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Campbell Ritchie wrote:
You don't have to apologize for being German. . . . .

That is a common problem...

Oh dear, it just keeps getting worse.

But enough of this German-bashing:
Andreas L. Berg wrote:I have to write down before every new request scanner.nextLine(); so that the slade quasi gets back to the begin of a new line. (so actually the behavior of the "return" key, or?)
Isn´t there a command that i can set after the first scanner, that sets the slade automatically back?
Something like scan.nextLine(){return};
...

You got it in one. (richtig)

BTW: 'slade' == 'cursor' (I think).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Winston Gutkowski wrote: . . .
Oh dear, it just keeps getting worse.
. . .
I hope you don’t think that was accidental

myScanner.nextLine(); is the simplest way to handle that problem.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

Andreas L. Berg wrote:
😳…well it was more meant that i post here code that is spiked with another language then english, not especially that im german…… ;o)
But thanks for your regard ^^


Sorry, about that! I was actually teasing you using a subcategory of humor known as intentional misapprehension. The teasing was meant to show you that you're welcome into the community, a technique I've seen applied successfully by others, but one I personally have had trouble executing successfully in the past. (c.f., the "Hey, Big Nose!" incident.) Of course, one might say that any attempt of humor to a German from another German (albeit a 5th generation mixed one) is pretty much doomed to failure from the outset, but at least my heart was in the right place.
Andreas L. Berg
Greenhorn

Joined: Oct 10, 2007
Posts: 8
myScanner.nextLine(); is the simplest way to handle that problem.


Ok, thats what i want to know. Thank you.

Sorry, about that! I was actually teasing you using a subcategory of humor known as intentional misapprehension.

You dont have to.
I could again excuse myself that i dont get the joke, you know germans are kinda humorless.
Ha, my Point. ;o)

No i guess jokes are hard to understand when you dont speak a language really good (what i dont do anymore), so i have to sorry that i messed it up…

But thank you again for your good advices about my problem and that im as a german been allowed in this forum ;o)

OK there is a american and italian and a german and the american say……… ;o)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with the Scanner Class