| Author |
Switch-Case
|
P Jain
Ranch Hand
Joined: Jul 31, 2004
Posts: 52
|
|
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.
|
Regards<br />P Jain
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
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:
|
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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.
|
Java API Documentation
The Java Tutorial
|
 |
sever oon
Ranch Hand
Joined: Feb 08, 2004
Posts: 268
|
|
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
Joined: Jul 31, 2004
Posts: 52
|
|
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
Joined: Jul 31, 2004
Posts: 52
|
|
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.
|
 |
 |
|
|
subject: Switch-Case
|
|
|