wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to ask for user input twice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to ask for user input twice?" Watch "How to ask for user input twice?" New topic
Author

How to ask for user input twice?

Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
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

Joined: Sep 20, 2010
Posts: 3605
    
  14

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

Joined: Oct 24, 2009
Posts: 20
its cool I fixed it with a little help

Mods can close this!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
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

Joined: Aug 11, 2010
Posts: 90
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

Joined: Oct 19, 2010
Posts: 4
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);
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to ask for user input twice?
 
Similar Threads
Write a program that reads and writes from binary or text files
Write a program that merges two files containing alphabetized lists of student records into a single
Question on searching ArrayLists
Scanner methods
how to write the game-CrossingRiver (2)