• 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Switch-Case

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic