This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to accept Multiline conditional input through console in java program.... 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 » Java in General
Bookmark "How to accept Multiline conditional input through console in java program...." Watch "How to accept Multiline conditional input through console in java program...." New topic
Author

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

Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
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 ]

********Deserve Before You Desire********
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
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.


Ping & DNS - my free Android networking tools app
Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
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

Joined: Jun 11, 2002
Posts: 140
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

Joined: Jun 07, 2007
Posts: 26
Here's a link I found very useful on this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
I always thought the way to implement that sort of menu is with a switch-case-break-default block.
 
Consider Paul's rocket mass heater.
 
subject: How to accept Multiline conditional input through console in java program....
 
Similar Threads
Adding a new array element is not saving to memory
Exception Handling
doubt in loop
how to take and check input in Java
calculate user input