Solved

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

Posted on 1998-07-22
7
220 Views
Last Modified: 2010-03-30
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() ) ;
  }
 
  }
}
0
Comment
Question by:sshah
  • 4
  • 2
7 Comments
 

Author Comment

by:sshah
ID: 1227549
Edited text of question
0
 

Author Comment

by:sshah
ID: 1227550
Adjusted points to 100
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1227551
Here is the note from the JDK 1.1 documentation:

Note: readLine() is deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form

DataInputStream d = new DataInputStream(in);
with
BufferedReader d = new BufferedReader(new InputStreamReader(in));

This then will allow the use of an "approved" kind of readLine.


0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:gadio
ID: 1227552
sshah, use InputStreamREader to read from the stdin, and then feed it to a StreamTokenizer to break the input to sperate numbers.
0
 

Author Comment

by:sshah
ID: 1227553
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??.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1227554
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.

0
 

Author Comment

by:sshah
ID: 1227555
Thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 61
couple of eclipse 5 45
Configure a Bean in an XML file 4 42
eclipse console opening separately 2 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

792 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