Results 1 to 8 of 8
- 08-20-2009, 06:29 PM #1
Member
- Join Date
- Aug 2009
- Location
- South Sweden
- Posts
- 24
- Rep Power
- 0
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... ;)
I know that i should add something toJava 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); }
But i've forgotten what, if nulltype.. no i dont knowJava Code:if (str.length() > 0)
Anyone with any sugesstion?
/Mattedatten
- 08-20-2009, 06:45 PM #2
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); }
-
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:
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.Java Code:if (str == null || str.trim().isEmpty()) {
- 08-20-2009, 06:57 PM #4
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); }
-
Mattedatten: I accidentally deleted one of your posts to this thread, sorry! Please repost if you can recreate.
- 08-20-2009, 07:09 PM #6
Member
- Join Date
- Aug 2009
- Location
- South Sweden
- Posts
- 24
- Rep Power
- 0
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
in front of JOptionPane? Our programming teacher haven't done that, and it doesn't seem to be needed?Java Code:javax.swing.
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
-
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.
- 12-04-2010, 11:09 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
jList Issues - getSelectedValue() Keeps returning "null"
By MoobKeeng in forum New To JavaReplies: 0Last Post: 07-28-2009, 06:45 PM -
Convert " ü " into simple " u "
By nitinb4u in forum New To JavaReplies: 4Last Post: 02-23-2009, 08:35 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Need help with "a simple order entry program"
By sentica in forum New To JavaReplies: 6Last Post: 10-17-2008, 05:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks