Results 1 to 8 of 8
  1. #1
    Mattedatten is offline Member
    Join Date
    Aug 2009
    Location
    South Sweden
    Posts
    24
    Rep Power
    0

    Default How to "allow null" in a very simple script?

    Hey all
    I know that i'm new here, but i found this forum, and i'm going to give it a try :D

    Anyway, i'm using my very basic java skills to create an input str, and if that str isn't longer than 0 or null (clicked "ok" without writing anything) then the program will stop, leaving an message "You didn't write anything, this program will exit"

    And of course, i don't like calling something like this a program, but... ;)
    Java Code:
    str = JOptionPane.showInputDialog(null, "How many MINUTES?", "Time", JOptionPane.QUESTION_MESSAGE);
    				if (str.length() > 0)
    				{
    					JOptionPane.showMessageDialog(null, "You didn't write anything, this program will exit");
    					System.exit(0);
    				}
    I know that i should add something to
    Java Code:
    if (str.length() > 0)
    But i've forgotten what, if nulltype.. no i dont know
    Anyone with any sugesstion?


    /Mattedatten

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Check for null on 'str' =)

    Java Code:
    str = javax.swing.JOptionPane.showInputDialog(null, "How many MINUTES?", "Time", javax.swing.JOptionPane.QUESTION_MESSAGE);
    
    if (str == null || str.length() == 0)
    {
       javax.swing.JOptionPane.showMessageDialog(null, "You didn't write anything, this program will exit");
       System.exit(0);
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    mrmatt shows a very important point in that your if statement should check if the String is null first before doing anything. If you don't, and the user closers your optionpane by clicking on the "x" in the upper right corner, your program will throw a very nasty NullPointerException if you try to call any methods on the String such as str.length since the String is null. The if boolean check will check from left to right, and if the String str is null the boolean statement will be true and no further checks will be done.

    Another way to do the if check if you also want to exclude strings that include nothing but white-space such as nothing but spaces:
    Java Code:
    if (str == null || str.trim().isEmpty()) {
    Trim will get rid of all white-space from the trailing and leading portions of the String if they are present, and isEmpty checks to see if the String is, well, empty. Happy coding and welcome to the forum.

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    You need to convert the string into an integer:

    Java Code:
    str = javax.swing.JOptionPane.showInputDialog(null, "How many MINUTES?", "Time", javax.swing.JOptionPane.QUESTION_MESSAGE);
    
    if (str == null || str.length() == 0)
    {
          javax.swing.JOptionPane.showMessageDialog(null, "You didn't write anything, this program will exit");
          System.exit(0);
    }
    
    int num = 0;
    try
    {
       num = Integer.parseInt(str);
    }
    catch(Exception e)
    {
       javax.swing.JOptionPane.showMessageDialog(null, "Not a number, this program will exit");
       System.exit(0);
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Mattedatten: I accidentally deleted one of your posts to this thread, sorry! Please repost if you can recreate.

  6. #6
    Mattedatten is offline Member
    Join Date
    Aug 2009
    Location
    South Sweden
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Mattedatten: I accidentally deleted one of your posts to this thread, sorry! Please repost if you can recreate.
    I realised something was missing :D

    No problem, i was going to re-edit that post myself, thanks mrmatt.

    I did paste your code over my if-part, and it worked, guess i forgot to edit something, thanks!

    but, why did you write
    Java Code:
    javax.swing.
    in front of JOptionPane? Our programming teacher haven't done that, and it doesn't seem to be needed?

    By the way, if anyone wonders, i was making an "scheme" to the rest of my school class, that prefers playing on the computers more than coming in good time to lessons :D

    I could post the code here, but:
    1. It's not done, neither tested
    2. It's long, ugly, and untrackable ;)
    //Mattedatten

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Mattedatten View Post
    but, why did you write
    Java Code:
    javax.swing.
    in front of JOptionPane? Our programming teacher haven't done that, and it doesn't seem to be needed?
    This tells Java where to find the JOptionPane class and its methods. If your class has import javax.swing.*; or import javax.swing.JOptionPane; at the very top, below the package declaration and above the class declaration then this fully qualified class name isn't needed.

  8. #8
    jLaurence is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 07-28-2009, 06:45 PM
  2. Convert " ü " into simple " u "
    By nitinb4u in forum New To Java
    Replies: 4
    Last Post: 02-23-2009, 08:35 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Need help with "a simple order entry program"
    By sentica in forum New To Java
    Replies: 6
    Last Post: 10-17-2008, 05:38 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
  •