• Post Reply Bookmark Topic Watch Topic
  • New Topic

My JibberJabber console program  RSS feed

 
Tyrel Richey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off just signed up and I love the moose and the agreement...be nice right on I think I am in the right place.

I have recently started learning java in anticipation of developing for android. I was making a program that would ask the user how many lines they wanted to jibber jabber then take that many lines of input and add it to an array list. Then randomly go through and print those strings back. My code works except one little glitch which I don't under stand. On the first pass of the for loop where it take the user input and adds it to the ArrayList it just displays the System.out.print(); but does not get the user input then on the second and all passes thereafter it works the way I wanted it to. Any insights would be very appreciated.

Here is the code its just one class and all in main. I know its bad practice but I am not planning on reusing my Jibber Jabber lol:

Also let me know if there is some way I am suposed to display code I am new to this forum.
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tyrel,

Welcome to Javaranch !

You can use the code tags to display the code. You said it works fine in the second and the subsequent iterations. It cannot go to for a second iteration unless the scanner has read some character. I am guessing it has read a space character.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused about this one. The scanner.nextLine() doesn't wait for input the first time. I'm really confused. Anyway you can use syntax highlighting with the code tag. Read more here: UseCodeTags. You can edit your post by pressing the button.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a guess, I am not sure since I can't test right now,

When you do scanner.nextInt and you type in a value then hit the enter key so the input gets flushed to the scanner. My guess is the nextInt method is consuming the integer but not the new line. Then the next time you try to consume a line it gets the new line character still left in the buffer from when you entered the integer. Try consuming the nextLine one prior to the for loop.
 
Tyrel Richey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:This is a guess, I am not sure since I can't test right now,

When you do scanner.nextInt and you type in a value then hit the enter key so the input gets flushed to the scanner. My guess is the nextInt method is consuming the integer but not the new line. Then the next time you try to consume a line it gets the new line character still left in the buffer from when you entered the integer. Try consuming the nextLine one prior to the for loop.

That was exactly what happened thank you to all that helped. Here is the working code:
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a poorly-documented thing about Scanner, which causes confusion to many people. I wrote about it here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!