?
Solved

reading standard input

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



Thanks
0
Comment
Question by:extasy888
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 26

Expert Comment

by:ksivananth
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() ;
0
 

Author Comment

by:extasy888
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
0
 

Accepted Solution

by:
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]);
         }
         
         
         
     }
     
}
0
 

Author Comment

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

Author Comment

by:extasy888
ID: 8230156
i got it working now. thx.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 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