Forums Register Login

reading a double

+Pie Number of slices to send: Send
Real beginner here. Started to experiment with some code. Originally had the values hardcoded into the program. Then I wanted to read the values in through the console. The following code compiles but does give the desired results.

public static void main(String [] args) throws IOException{
DataInputStream in = new DataInputStream(
System.in);
System.out.println("Enter beginning balance:");
ip_value = in.readDouble();
System.out.println(ip_value);
TestAccount account = new TestAccount();
NumberFormat dollarformat = NumberFormat.getCurrencyInstance();
System.out.println("beginning balance = " + dollarformat.format(ip_value));
2000 was entered.
results:
6.004549348750547E-67
beginning balance = $0.00
+Pie Number of slices to send: Send
If you look at the Java API for java.io.DataInputStream you'll see that it reads native java data as bytes. The "readDouble()" method indicates that it'll read the next eight bytes and interpret it as a double (based on the IEEE 754 spec). This is not what you want.
You want to read in the characters you type and convert that to a double. try this:

And as a defensive measure, you'll probably want to put the "parseDouble()" call inside of a try/catch block.
[ March 08, 2004: Message edited by: Wayne L Johnson ]
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 389 times.
Similar Threads
permitted access violation?
arrayList again
Java error
Synchronization Issue With Threads.
Using synchronized still doesn't solve the "lost update" problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:00:10.