aspose file tools*
The moose likes Beginning Java and the fly likes how to read character from command line and store it string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to read character from command line and store it string" Watch "how to read character from command line and store it string" New topic
Author

how to read character from command line and store it string

Anumeha Taori
Greenhorn

Joined: Oct 25, 2009
Posts: 3
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 25, 2009
Posts: 3
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

Joined: Sep 29, 2008
Posts: 12617

Command line arguments are available in the public static void main(String[] args) parameter.
Anumeha Taori
Greenhorn

Joined: Oct 25, 2009
Posts: 3
How System.in.read() is used to read the characters and store it in string ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not, for the command line arguments.
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

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


SCJP 5.0 SCWCD 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

Unlike the C function getc(), System.in.read() will not return anything until the user has pressed enter. Keep that in mind.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to read character from command line and store it string