• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How to accept Multiline conditional input through console in java program....

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am writing a java class for accepting conditional user input through console as follows:-
+++++++++++++++++++++++++++++++++++++++++++
Enter your choice as below:-
Press '1' for add new item.
Press '2' for add edit item.
Press '3' for add remove item.
+++++++++++++++++++++++++++++++++++++++++++

Now if someone enters values other than 1,2 or 3 it should display message as follows:-

+++++++++++++++++++++++++++++++++++++++++++
4
You have entered invalid input, Please enter again!!!
1
Enter Item Details:-

& So on.....
+++++++++++++++++++++++++++++++++++++++++++

Could you please help me how to achieve this?

I want a conditional input, i.e, if user enters invalid input then it should prompt him for entering again one of above commands.

Many thanks in advance.

Pras.
[ March 28, 2008: Message edited by: Pras Kumar ]
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using Java 6 you can use the java.io.Console class for this.

Here's an example of how to do the same without the help of any classes or libraries.
 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I am using JDK 1.4 for this. Any way of doing this thing in Java versions prior to Java 6?
 
Ranch Hand
Posts: 140
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should do it...



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



public class Menu {

public static void main(String[] args) {
try {
new Menu().run();
} catch (IOException ioe) {
ioe.printStackTrace();
}

}

public void run() throws IOException {
showMenu();
String input = getInput();
while (!isInputValid(input)) {
showMenu();
input = getInput();
}

System.out.println("Congrats, you entered valid input. now onto the next step.");

}

public void showMenu() {
System.out.println("Press 1 for add new item");
System.out.println("Press 2 for add edit item");
System.out.println("Press 3 for add remove item");
}

public String getInput() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
return in.readLine();
}

public boolean isInputValid(String in) {
if (in.equals("1") || in.equals("2") || in.equals("3")) {
return true;
}

return false;
}

}
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a link I found very useful on this.
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always thought the way to implement that sort of menu is with a switch-case-break-default block.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic