DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005

    Unhappy How can I read two double after typing?

    Hi everyone!!

    Thank you for reading me, I need your help.
    I'm new (very very new) in JAVA programming.

    I have a problem. I'm trying to write an application that reads a couple of
    numbers (double). It shows a message on screen that says something like
    'type a number', and then it reads it.

    The thing is that the program doesn't stop for the input of the second
    number and I don't know how to solve it.

    Can you help me please?

    My code is similiar to the following one:

    System.out.println("Type the first number: ");
    x = System.in.read();
    } catch(IOException e){
    System.out.println("It didn't work.");

    System.out.println("Type the second number: ");
    y = System.in.read();
    } catch(IOException e){
    System.out.println("It didn't work.");
    // We build the point
    Point point1 = new Point(x,y);

    Thank you very much for your attention and help.

  2. #2
    Join Date
    Aug 2003
    The System.in.read() method doesn't read data as text, it reads raw data from the console. In other words, if you were to type 1, you would get the ASCII value of '1', not 1. This said, there are several alternatives for reading data from files (or the console) as text. First, you can get the Scanner class which I notice a lot of people using, Its new to Java 1.5, so I don't know a whole lot about it, but you can check the man pages (http://java.sun.com/j2se/1.5.0/docs/.../Scanner.html). Alternatively, you can used BufferedReader and Double.parseDouble(), but this requires that you know things like both numbers are on the same line and stuff like that. For strictly formatted input, it works well, but otherwise it is kinda difficult to use. If both numbers were going to be on the same line, you might do something like this:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String ln = in.readLine();
    StringTokenizer st = new StringTokenizer(ln);
    double x = Double.parseDouble(st.nextToken());
    double y = Double.parseDouble(st.nextToken());
    Hope that helps.

  3. #3
    Join Date
    Nov 2005

    Thank you!


    Thank you very much for your help, I'm going to try your suggestion.

    Thank you for your help.


Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. Need Help With Multi Function Program
    By WackoWolf in forum C++
    Replies: 4
    Last Post: 10-17-2005, 04:22 PM
  3. Replies: 3
    Last Post: 10-02-2005, 11:57 PM
  4. Mortgage calculator help
    By vwrado in forum Java
    Replies: 1
    Last Post: 09-26-2005, 05:09 PM
  5. Double Text 1.0
    By George Gilbert in forum vb.announcements
    Replies: 0
    Last Post: 08-19-2001, 11:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.