Solved

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

Posted on 1998-07-22
7
190 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now