Results 1 to 5 of 5
  1. #1
    trudy is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Question TextIO class not working

    I have 4 TextIO input statements in my program. The first 3 work, but the fourth one doesn't work I can't figure out why. It displays the heading "Enter another item? (Y/N) "..... but doesn't wait for any input, it just continues.

    System.out.print("Enter item number: ");
    String itemNumber = TextIO.getlnString();

    /* convert lower case to upper case */
    itemNumber = itemNumber.toUpperCase();

    itemCount=+1;

    System.out.print("Enter item description: ");
    String itemDescription = TextIO.getlnString();

    System.out.print("Enter Price: ");
    double unitPrice = TextIO.getDouble();

    System.out.println("Enter another item? (Y/N) ");
    String moreSt = TextIO.getlnString();


    System.out.print("Enter qoh: ");
    double qtyOnHand = TextIO.getDouble();

    The "Enter qoh" prompt works, but the "Enter another item" doesn't wait for user input.
    I have no idea what is wrong, any help is appreciated!!:confused:
    Last edited by trudy; 12-30-2009 at 02:42 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You would need to read the documentation of your TextIO class to find out what is going on when it reads a double. e.g Are you giving it the double that it is expecting at all?

    Much better is to throw away that TextIO class and use the Scanner class (part of standard JavaSE since version 1.5).

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I agree with r\d*x you would be much better off using a standard Java class.

    If you are not going to then you should provide a link if you post on a public forum like this. Anyway Google turned up the following for getDouble():

    /**
    * Skips whitespace characters and then reads a value of type double from input. Any additional characters on
    * the current line of input are retained, and will be read by the next input operation. When using standard IO,
    * this will not produce an error; the user will be prompted repeatedly for input until a legal value
    * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found.
    */

    When it talks about "additional characters on the current line of input" that includes the newline character at the end of the line. So what happens is that you read the double leaving the newline in place and the following getlnString() dutifully reads the end of line character and assigns an empty string to moreSt.

    Perhaps there is another method in this class that reads a double and then discards the rest of the line (including the next end-of-line character). Hint your code uses getlnString(). What do you think the "ln" might be for?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            System.out.print("Enter Price: ");
            double unitPrice = TextIO.getDouble();
            // Try adding this line to consume the newLine.
            TextIO.getln();
    
            System.out.print("Enter another item? (Y/N) ");
            String moreSt = TextIO.getlnString();
            System.out.println("moreSt = " + moreSt);

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    // Try adding this line to consume the newLine.
    TextIO.getln();

    Note that under some circumstances this approach will lead to a result that differs from using the correct double reading method in the first place. (see question posed at the end of #3)

Similar Threads

  1. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  2. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •