• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to ask for user input twice?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code for the user input



this is just a snippet. When I run the program everything works fine except it says ''Please enter two words: ". Then after I enter a word it skips down
to a blank line and that is where I enter the 2nd word. The problem is the user will never know to enter the 2nd word on that 2nd blank line.
My question is how do I get it to ask for it twice. For ex:

Please a word:
Please enter another word:



like this ^^^^

Even though I have it as 'please enter two words' that will confuse the user because the user will input two words in the first line but Java will take both words as 1.

Thanks in advance
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what *do* you want? Do you want the user to enter both words on the same line? In that case, use Scanner.next() instead of Scanner.nextLine().
 
Tom Little
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its cool I fixed it with a little help

Mods can close this!
 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunny Sarow wrote:its cool I fixed it with a little help

Please tell us how; somebody else might have the same problem in future.



Mods can close this!

No, we don't usually close threads. They stay open for ever.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you just said,

Please a word:
Please enter another word:



so you may put the System.out.println() here and there.



 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried this code ...it works on my side ....hope it will behave similar on your side....i tried it on java-eclipse too.....


import java.io.*;

public static void main(String args[]) throws IOException{
String str1,str2;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("enter two lines...");
str1=dis.readLine();
str2=dis.readLine();

System.out.println("You have entered:");
System.out.println(str1+" AND");
System.out.println(str2);
}
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic