• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

My JibberJabber console program

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Marshal
Posts: 80872
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a poorly-documented thing about Scanner, which causes confusion to many people. I wrote about it here.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic