• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read input from command line

 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try something like this:
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic