Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Read input from command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read input from command line" Watch "Read input from command line" New topic
Author

Read input from command line

Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
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


Raising Flares debut album 'Ignition' out now

http://www.raisingflares.com

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
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

Joined: May 30, 2001
Posts: 328
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

Joined: Sep 29, 2000
Posts: 8521
Try something like this:


"JavaRanch, where the deer and the Certified play" - David O'Meara
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
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


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
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

Joined: Sep 29, 2000
Posts: 8521
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.
 
 
subject: Read input from command line
 
Similar Threads
function call
class 1
Taking interactive input from console
Input on command line
Help(Client Fails to read from Server)