Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Read double value from console

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    26
    My Mood
    Fine
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Angry 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:
    import java.io.*;
    class inputcheck{
    public static void main(String args[]) throws IOException {
    double a,b;
    //DataInputStream br=new DataInputStream(System.in);
    ObjectInputStream br=new ObjectInputStream(System.in);
    System.out.println("Enter:");
    a=br.readDouble();
    b=br.readDouble();
     
    System.out.println("a is: "+a+" and b is: "+b);
    }}

    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.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Read double value from console

    Quote Originally Posted by Junky View Post
    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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. The Following User Says Thank You to JavaPF For This Useful Post:

    Lord Voldemort (July 12th, 2011)

  5. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    26
    My Mood
    Fine
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read double value from console

    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?

  7. #6
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Read double value from console

    Quote Originally Posted by Lord Voldemort View Post
    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.

  8. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    26
    My Mood
    Fine
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Smile Re: Read double value from console

    Quote Originally Posted by Norm View Post
    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.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read double value from console

    How do I read the API doc?
    Here: Java Platform SE 6

  10. The Following User Says Thank You to Norm For This Useful Post:

    Lord Voldemort (July 12th, 2011)

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. Console box with scanner?
    By Hallowed in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2011, 12:50 AM
  4. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  5. How do i run this Accounting console app?
    By mirzahat in forum AWT / Java Swing
    Replies: 2
    Last Post: November 16th, 2010, 12:22 AM

Tags for this Thread