Link to home
Start Free TrialLog in
Avatar of sshah
sshah

asked on

How do I read char,int,double, string from a keyboad ( Terminal ).

Hi :

How do I read Integer, Double, Char and String data type cleanly from the Keyboard Terminal. I have a program which requires user input and I dont want to use Applet for testing. I can do this in a crude way but I want a clean way to handle this.

What I have done to handle this is to read each line and then parse it using StringTokenizer to read doubles. But readLine in DataInputStream is also a depreciated. So I really dont want to use this. Is there a clean method to do this??

Also How do I handle if I have two double on one line seperated by space or two double on two seperate lines??

I have a following sample program but it does not work.
import java.io.*;
public class IOTest
{
  public static void main(String args[])
  {
    DataInputStream dis = new DataInputStream ( System.in );
    double d = dis.readDouble() ;
     System.out.println ( "Double d = " +d1  ) ;
  } catch ( IOException io )
  {
      System.out.println ( "readDouble Exception: "+io.toString() ) ;
  }
 
  }
}
Avatar of sshah
sshah

ASKER

Edited text of question
Avatar of sshah

ASKER

Adjusted points to 100
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sshah, use InputStreamREader to read from the stdin, and then feed it to a StreamTokenizer to break the input to sperate numbers.
Avatar of sshah

ASKER

Good Info. But Is there a better way to read compared to reading it as a string and then converting that string to double?? Can I directly read it as a double?? Can I use any of the methods of the of the interface readBoolean, readChar, readFloat or readDouble??. Or any other way to go around rather then reading first string and then converting to appropriate type??.
No. The readDouble stuff is a portable way of retreiving data from a file. The expected format is some kind of binary (produced by a corresponding write type call of DataOutputStream). When acquiring information from the console you are inherently dealing with character data.

Avatar of sshah

ASKER

Thanks.