devxlogo

Reading a Double from Command Line

Reading a Double from Command Line

Question:
Hi, I just started to program in Java in Win95 platform. Even though I/O through files has already been discussed, I am having trouble reading a double from the command line and then writing that same number with .writeDouble() and System.out.println();

Here is the code of the program:

import java.io.*;public class IOtest {        public static void main(String args[]) {                         try {           System.out.println(“Enter double”);           DataInputStream istream = new DataInputStream(System.in);           DataOutputStream ostream = new DataOutputStream(System.out);                      double p = istream.readDouble();           System.out.println(“This is with ostream”);           ostream.writeDouble(p);           System.out.println(“This is with System.out ” + p);                   }        catch(IOException e) {                System.out.println(e.getMessage());        }     }}
When I run this program, I enter a number and only after I press Enter five times, is that the program continues and surprisingly, it this is what comes out:
This is with ostream1.5 This is with System.out 8.54833e-072

Answer:
Yours is a very common confusion. When you read a float or double via DataInputStream, what Java expects is just that: binary data in IEEE 754 format. Accordingly, what you have to do is to read in the user input as a string, and then use conversion functions to produce your floats or doubles. I have modified your program to do this:

import java.io.*;public class IOtest {        public static void main(String args[]) {                         try {           System.out.println(“Enter double”);           DataInputStream istream = new DataInputStream(System.in);           DataOutputStream ostream = new DataOutputStream(System.out);           PrintStream postream = new PrintStream(ostream);                      //double p = istream.readDouble();           String line = istream.readLine().trim();           double p = (new Double(line)).doubleValue();           System.out.println(“This is with ostream”);           ostream.writeDouble(p);           System.out.println(”
This is with PrintStream”); postream.print(p); System.out.println(”
This is with System.out ” + p); } catch(IOException e) { System.out.println(e.getMessage()); } }}

In the CD-ROM included with Cay Horstmann’s “Core Java” book, you can findtwo classes that can help you for both input and output conversions: Consoleand Format, respectively.

See also  11 Effective Keyword Research Tools and Techniques
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist