• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a String

 
Othmar Stehlik
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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.
Thanx
Othmar
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Othmar
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

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Khurram Akhter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
Khurram
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a sample of requesting input during an application - without a gui.

 
Othmar Stehlik
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanx to all who posted this usefull links and examples!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic