This week's book giveaway is in the Functional programming forum.
We're giving away four copies of A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles and have Ben Weidig on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • 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: 14859
334
  • 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: 77957
373
  • 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