dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Beginner
Mar 19, 1997

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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("\nThis is with PrintStream");
           postream.print(p);
           System.out.println("\nThis 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 find two classes that can help you for both input and output conversions: Console and Format, respectively.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date