Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 13479
304
  • 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: 74627
335
  • 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);
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic