Results 1 to 8 of 8
  1. #1
    okletsgo is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Question Trouble getting program to recognize when the "ENTER" key is selected.

    Greetings everyone. I am very new to Java and going through a Java course book. For each example they have in the book I try to make modifications to it to ensure I am understanding the lesson. I have come to a program now tought which I cannot manipulate the way I want to and I do not understand why. Basically the program collects timeframes from a user which correlate to the number of hours of sleep each person in a dorm gets and then it calculates the average sleep time for the dorm. To end the input of hours the user selects "0" and this tells the program to move forward with the calculations for the average. The original program had code to catch if the user entered "0" as the very first input for sleep hours and would print out a message of "no data entered". My goal is to recognize when nothing at all is entered (user just presses the "ENTER" key without any value beforehand) and print out a message indicating that the user did not enter any values at all. I figured the best approach to dong this was to do a comparison of the input variable to "null" which is what memory would have stored if no actual value was entered before pressing the "ENTER" key. MY modification is not working though. When I press the "ENTER" key by itself I get an "Exception" message and my program just locks up. Any help with this would be greatly appreciated!!!

    Here is the code, my modifications are identified near the bottom:

    import javax.swing.*;
    import java.text.*;


    class Ch6SleepStatistics {
    static JFrame myframe = new JFrame();


    public static void main (String[] args) {
    myframe.setSize(300, 300);
    myframe.setVisible(true);

    Ch6SleepStatistics prog = new Ch6SleepStatistics();
    prog.start();
    System.exit(0);
    }

    public void start() {
    double sleepHour, sum = 0;
    int cnt = 0;

    //enter the dorm name
    String dorm = JOptionPane.showInputDialog(myframe, "Enter the dorm name: ");

    //loop: get hours of sleep for each resident until 0 is entered
    sleepHour = getDouble("Enter sleep hours (0 = stop): ");

    while (sleepHour != 0) {
    sum += sleepHour;
    cnt++;
    sleepHour = getDouble("Enter sleep hours (0 = stop): ");
    }

    //flag if "0" is pressed as the first value
    if (cnt == 0) {
    JOptionPane.showMessageDialog(myframe, "No data entered");
    } else {
    DecimalFormat df = new DecimalFormat("0.00");
    JOptionPane.showMessageDialog(myframe, "Average sleep time for " + dorm + " is \n\n " +
    df.format(sum/cnt) + " hours.");
    }

    }

    //method to get feedback from user
    private double getDouble(String message) {
    double result, sleepHour;

    String str = JOptionPane.showInputDialog(myframe, message);
    result = Double.parseDouble(str);

    //************************************************** ***************
    //my code for testing if the "Enter" key was pressed without any previous number entered
    //************************************************** ***************
    String stringTest = Double.toString(result);
    if(stringTest.equals(null)) {
    System.out.println("You did not enter any value");
    sleepHour = getDouble("Enter sleep hours (0 = stop): ");
    }
    //************************************************** ****************
    return result;
    }

    }


    here is a copy of the Exception error message:

    Exception in thread "main" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:10
    11)
    at java.lang.Double.parseDouble(Double.java:540)
    at Ch6SleepStatistics.getDouble(Ch6SleepStatistics.ja va:55)
    at Ch6SleepStatistics.start(Ch6SleepStatistics.java:3 1)
    at Ch6SleepStatistics.main(Ch6SleepStatistics.java:19 )

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    Please use [code] tags [/code] when posting code as that's pretty much unreadable without them.
    From the stack trace, when the user simply hits ENTER the code is still trying to turn that into a double.
    You need to add a step somewhere to check whether the String entered is empty or not.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    okletsgo is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    Please forgive my ignorance here but I am truly new to Java programming and programming in general. I do not understand what you mean by "code tags". If you can explain this term to me I will be sure to do it in future postings.

    As far as the reply though I thought that the code "if(stringTest.equals(null))" was doing the check on whether the input (in this case the object "stringTest") is empty. It is my understanding that the method "equals" compares the actual string object's sequence of characters to what we designate. In my case I compared the string object to "null" which means no characters. So I was thinking that the "stringTest" object would have no sequence of characters in it which would match the "null" reference. But it isn't working so I am obviously misunderstanding the method functionality of the "equals" method. Any further pointers would be greatly appreciated. Thanks.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    A null String and an empty String are completely different. In fact there is no such thing as a null String. Only the null reference.
    Java Code:
    String a = null;
    String b = ""; //an empty String

  5. #5
    stevenfriz is offline Member
    Join Date
    Feb 2012
    Posts
    28
    Rep Power
    0

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    Code tags mean that you use the HTML tags "["code"]" and "["/code"]" (without quotes) before and after you input your code. This organizes it as if you were in an IDE.
    Junky's reply is true, you cannot have a null String. You can have null int's or objects, for example. As for whether or not that null error is the problem, I cannot tell you.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    I didn't read the code (I don't tend to without code tags), but Junky has spotted where you are going wrong.
    Indeed that line:
    Java Code:
    if(stringTest.equals(null))
    will do one of two things. It will either be false if stringTest is not null or throw a NullPointerException id stringTest is null.

    Besides, you are doing this check after the attempt to turn the string into a Double, so even if it were a valid test (checking for an empty String rather than a null one) it stil wouldn't help as it's in the wrong place.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    okletsgo is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    Many thanks to everyone's replies. I am not familiar with HTML tags but I will read up on it so as not to input future posts incorrectly :) I will read up further regarding empty strings. Thanks again.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Trouble getting program to recognize when the "ENTER" key is selected.

    Quote Originally Posted by stevenfriz View Post
    You can have null int's
    Really???

    Please show how this is possible.

Similar Threads

  1. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  2. Replies: 1
    Last Post: 12-09-2011, 01:18 PM
  3. output "left click"/enter command?
    By Arsenic in forum New To Java
    Replies: 2
    Last Post: 06-04-2009, 05:01 AM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •