Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read input from command line

 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to practice string and variable handling in small apps.
I'd like to know how to read input from the command line once an apps running (I know how to pass in args at the start).
I'm thinking along the lines of C++'s ReadFloatPr(" ....");
Thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by 'from the command line'

The only way a java application can read from the command line is through any parameters passed to it...

If you are asking, instead, how to read input from, or write to the console ( the DOS window you are running your java program in ), you would use System.in and System.out...

HTH,
-Nate
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that's it.
So how would I pass a char from System.in to a switch statement?
Would it be like this?
switch (System.in){
case 'a': println("You typed 'a'!");
case 'n': .........
}
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget I said that!!!

Obviously switch has to have a char, short, int etc as a variable.
So how do I create that variable?
char u = System.in(); or something like that?
Thanks,
Terry
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this:
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another very simple example I got from an online source -sorry, don't remember where
class Echo
{
public static void main (String [] args) throws java.io.IOException
{
int ch;
System.out.print ("Enter some text: ");
while ((ch = System.in.read ()) != '\n')
System.out.print ((char) ch);
}
}
This will echo back up to a full line of text (note the '\n').

------------------
I'm a soldier in the NetScape Wars...
Joel
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both very much. I seem to be having the same problem with both examples though.
I've copied your codes, compiled them without and problems but on trying to run them they both give this error

Exception in thread "main" java.lang.NoClassDefFoundError:
followed by the name of the compiled file.
Further help is needed by this beginner.
Thanks,
Terry
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sound like you need to fix up your classpath. This is what tells the JVM where to go look for all the class to use.
In DOS you would say something like:
SET CLASSPATH=c:\jdk1.3.0\bin;c:\jdk1.3.0\MyworkingDir
You can put this statement in either your autoexec.bat or in a .bat file and execute it in DOS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic