This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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;
}

}
 
Neelesh A Korade
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link I found very useful on this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought the way to implement that sort of menu is with a switch-case-break-default block.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic