Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to ask for user input twice?

 
Tom Little
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
 
Stephan van Hulst
Bartender
Pie
Posts: 5813
61
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
its cool I fixed it with a little help

Mods can close this!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • 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.
 
J. Insi
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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.



 
garg sumit
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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);
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic