Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


reading standard input

Posted on 2003-03-28
Medium Priority
Last Modified: 2010-03-31
how can i read a character at a time from standard input and put that character into an array?

Question by:extasy888
  • 3
LVL 26

Expert Comment

ID: 8229424
I think you can not read charecter by char as the reader will be waiting to recieve the enter pressed. But once the if the enter pressed, you can read the inputs by char by char,

try the folowing,

DataInputStream dis = new DataInputStream( System.in ) ;
char val = 0 ;
StringBuffer sb = new StringBuffer() ;

while( ( val = dis.readChar() ) != -1 )
  sb.append( val ) ;

char arr[] = sb.toString().toCharArray() ;

Author Comment

ID: 8229496
hi ksviananth
because i'm new to java, and still not too sure how to implement what u typed out for me, could you please write out the entire code for implementing the code you provided above? thanks for the inconvenience

Accepted Solution

AIndy earned 60 total points
ID: 8230026
import java.io.*;

class CharIn

     public static void main(String[] args)
     throws IOException
          // Replace 2 with the desired length of your char-array
          char[] charArray = new char[2];
          BufferedReader reader = new BufferedReader(new InputStreamReader( System.in ));    
          for (int i = 0; i<charArray.length; i++)
              charArray[i] = reader.readLine().toCharArray()[0];
         } // for i
          System.out.println ("You entered : ");
          for (int i = 0; i<charArray.length; i++)
              System.out.print (charArray[i]);

Author Comment

ID: 8230110
I get Exception in thread error. what may be the problem?

Author Comment

ID: 8230156
i got it working now. thx.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question