File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Reading a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a String" Watch "Reading a String" New topic

Reading a String

Othmar Stehlik
Ranch Hand

Joined: Jul 24, 2001
Posts: 36
Can someone please give me an explicit example, hiw to read a String from the command ine?
I�m still confused with the different type of Inputstreams, and how to apply them.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
When your main method executes it is sent all of the command line arguments (arguments after the class name) as an array of strings referenced by the args variable. Then you can access them by just refering to the position in the array of the argument you want. For example, say you have a class called Test, that looks like this:

If you run it with a command line entry like this:
java Test 1 26 Fred
Then your output would be:

Hope that helps

Sun Certified Programmer for the Java� 2 Platform

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
You don't need InputStreams to read from the command line. Here is a link with more information on getting arguments from the command line.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Khurram Akhter

Joined: Oct 08, 2001
Posts: 3
Whenever you enter any value through command line. its in a form of string and Storing (String[]args) -----> there...
so, if you want to print these words...just write..
System.out.println("The Values are "+args[0]);
If you enter more then one values just put a loop in your code, like
for (int i=o;i<args.lenght;i++) {>
System.out.println("The Values are "+ " " + args[i]);
Bye Happy Coding...
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Here is a sample of requesting input during an application - without a gui.

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

Joined: Jul 24, 2001
Posts: 36
Thanx to all who posted this usefull links and examples!
I agree. Here's the link:
subject: Reading a String
It's not a secret anymore!