Read double value from console
Some time ago I wrote a program to read double values from console using ObjectInputStream. Then I made some changes to it and recompiled. Now I can't remember how it used to work. I know the BufferedReader way but I'm going insane trying to make this program work. Here it is:
The working program must have been much like this.
I tried a lot of things but I can't figure out what's wrong with it.
This one compiles correctly but doesn't work.
Please help.
Re: Read double value from console
"doesn't work" provides zero information. Does it wander off and make a cup of tea? Does it shut your computer down? Does it generate a black hole?
In any case you can use the Scanner class which makes it very easy to get user input from the command line.
Re: Read double value from console
Quote:
Originally Posted by
Junky
In any case you can use the Scanner class which makes it very easy to get user input from the command line.
http://www.javaprogrammingforums.com...ner-class.html
Re: Read double value from console
I've got a working example of Scanner but I'm trying to remember how this program worked.
Details about "doesn't work":
There's no error on compilation. On running, "Enter " is not displayed and the program keeps waiting for something. If I type in some number and press enter, there's a StreamCorruptedException.
Re: Read double value from console
Quote:
read double values from console using ObjectInputStream
Have you read the API doc for this class?
Is there anything about what the class is documented to do that makes you think it can read console input?
Re: Read double value from console
Quote:
Originally Posted by
Lord Voldemort
Some time ago I wrote a program to read double values from console using ObjectInputStream. Then I made some changes to it and recompiled. Now I can't remember how it used to work.
Use comments in your code, with out them it looks like Spaghetti code. Just remember that that every one else code to you looks like Spaghetti code and 3 months later your code looks like Spaghetti code to you.
Meaningful comments and naming structure helps make everyone's code more readable.
Re: Read double value from console
Quote:
Originally Posted by
Norm
Have you read the API doc for this class?
Is there anything about what the class is documented to do that makes you think it can read console input?
How do I read the API doc? This program worked when I wrote it a few months back. It might have worked with DataInputStream (it's commented out now) but I can't be sure or else I wouldn't have posted this thread. What I am sure of is that I used DataInputStream or ObjectInputStream and I didn't have to write code to convert string value to double value as is done when using BufferedReader or Scanner.
This fiasco does indeed make a strong case on favour of comments as william points out, but I might have been pressed for time when the wrote the program. Sometimes I just can't wait to try out a new way of doing things.
Re: Read double value from console
Quote:
How do I read the API doc?
Here: Java Platform SE 6