File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch-Case

 
P Jain
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
How do we take input from command prompt in Switch-Case.
I want to check some value and if it is equal to some specific value it should take input from command propmt.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keep in mind that a switch statement can only deal with integer values, so you'll need to convert all the values from the command line that you're evaluating to ints:

 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the way I read the question, the subject line doesn't match. If you are asking about how to actually get the input from the command line, here's a simple example:

After compiling this program, an example run might look something like this:

I hope this helps illustrate how to access command line arguments. If you have any more questions, please feel free to ask.

Keep Coding!(TM)

Layne

Disclaimer: Use the code and output in this post at your own risk. I didn't take the time to compile it, so no warranty is given, explicit or implied, by its use.
 
sever oon
Ranch Hand
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This program, called ParseCmdLine, provides you with a basis from which you can build your own UNIX-style command line parser.



It accepts one command line argument of each type: a word argument, an argument that requires an argument, and two flags. In addition, this program requires a filename. Here's the usage statement for this program:
usage: ParseCmdLine (-verbose) (-xn) (-output afile) filename

The arguments within parens are optional; the filename argument is required.
 
P Jain
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Thanks for all the replys.
I think the statement of the question submitted by me was not clear.
I want to know how can we take user input in Case statment like....


switch(choice)
{
case 1:
System.out.println("Enter your value :");
DataInputStream dis = new DataInputStream(System.in);
String str = dis.readLine();
System.out.println(str);
break;

case 2:
//some working
 
P Jain
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Thanks for all the replys.
I think the statement of the question submitted by me was not clear.
I want to know how can we take user input in Case statement like....


switch(choice)
{
case 1:
System.out.println("Enter your value :");
DataInputStream dis = new DataInputStream(System.in);
String str = dis.readLine();
System.out.println(str);
break;

case 2:
//some working
break;
}
whenever I use this type of code control will skip the case statement if choice is 1,it will not stay for taking user input.

how do I forced it to take user input in case 1.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic