This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read character from command line and store it string

 
Anumeha Taori
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !!

I want to read character from command line and store it in string one by one.
Please tell me the way to implement it.

Can I use System.in.read() for it ?
 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This appears to be a serious question about Java and not meaningless drivel at all. So I'm going to move it to the Java beginners forum. I'm sure that if Anumeha meant it to be a joke, he or she will let us know and it can be moved back.
 
Anumeha Taori
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh...my mistake !!!
I am new to this forum. I didn't knew to which forum should I post and I posted it randomly. I didn't meant to joke. Sorry !!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Command line arguments are available in the public static void main(String[] args) parameter.
 
Anumeha Taori
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How System.in.read() is used to read the characters and store it in string ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not, for the command line arguments.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.in is a standard input stream just like System.out which is a standard output stream.it is a byte stream with no character stream features.

to use them efficiently we should wrap them using InputStreamReader or further BufferedReader.

for getting input from command line you can write

BufferedReader br = new BufferedReader ( new InputStreamReader ( System.in ) ) );
String commandInput = br.readLine( ) ;

have a look


avi sinha
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using a Java version less than 5 years old, you will have the Scanner class available. This allows easier access to inputs, including System.in. It is much easier to use than System.in.read(). It has various nextXXX() methods, which allow you to read a particular type of input, but not a nextChar() method. You can try next().charAt(0). But most of the time, you don't really want an individual char, you want a "word".
There is a potential confusion with Scanner, that it can return empty Strings; I commented on that problem here a few weeks ago.

If you want a String, the next() and nextLine() methods of the Scanner class are probably what you want.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike the C function getc(), System.in.read() will not return anything until the user has pressed enter. Keep that in mind.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic