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

how to read character from command line and store it string

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How System.in.read() is used to read the characters and store it in string ?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not, for the command line arguments.
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79984
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic