Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with the Scanner Class

 
Andreas L. Berg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic