Results 1 to 12 of 12
Thread: start again after no data entry
- 06-15-2011, 07:35 AM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
start again after no data entry
Hello,
I want to start the program from the beginning if upd.equals is left empty as now I am having an error.
Thanks !Java Code:import javax.swing.JOptionPane; //import java.util.Scanner; public class marks { public static void main(String[] args) { //Scanner scan = new Scanner(System.in); int marks = 0; for (int x=1; x<11; x++){ String upd = JOptionPane.showInputDialog("Please enter mark number " + x); if (upd.equals("")){ JOptionPane.showMessageDialog(null, "Please Enter a Mark ! "); x--; }else{ System.out.println("Proceed"); } int upd1 = Integer.parseInt(upd); marks = marks + upd1; //System.out.println(upd1); } JOptionPane.showMessageDialog(null, "The Total mark is " + marks); } }
- 06-15-2011, 07:39 AM #2
- 06-15-2011, 08:03 AM #3
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Java Code:Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at marks.main(marks.java:28)
- 06-15-2011, 08:12 AM #4
Obviously "" (an empty string) is not a valid int and cannot be parsed. You do a check to see if upd is equal to "" so perhaps you should only parse upd if it is not "". Along the same lines what will your code do if user enters "Fred"?
- 06-15-2011, 08:58 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
"" and null are not the same. You are not checking upd if it is null. In this case, upd is null but
since your just checking if upd is equals to "" then it continue to parse upd to integer eventhough it is nullLast edited by mine0926; 06-15-2011 at 09:04 AM.
- 06-15-2011, 03:39 PM #6
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
thanks but how do I parse only if upd is not equals to ""
- 06-15-2011, 03:50 PM #7
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
ok found out, just copied the parse section in the else section inside the if condition as it was out !
- 06-15-2011, 03:53 PM #8
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
to make the program better as you told me:
Along the same lines what will your code do if user enters "Fred"?
how can I check the string only to accept values from 1 - 100 ?
- 06-16-2011, 01:16 AM #9
- 06-16-2011, 07:43 AM #10
- 06-16-2011, 08:01 AM #11
I cry foul. Well I was talking about getting user input. Interesting it also returns null if the Cancel button is clicked.
- 06-16-2011, 09:18 AM #12
As documented, but unfortunately only for one of the many overloads.
dbLast edited by DarrylBurke; 06-16-2011 at 09:30 AM. Reason: Danged link parser doesn't understand []
Similar Threads
-
Check one array entry to each other entry in another array?
By javadingbat in forum New To JavaReplies: 10Last Post: 02-11-2011, 08:15 PM -
Data Entry Workers Needed Worldwide
By rajshree in forum Jobs OfferedReplies: 0Last Post: 01-30-2011, 06:10 AM -
Save data entry
By tiger100plus in forum NetBeansReplies: 2Last Post: 12-08-2009, 02:53 PM -
Make money from home, Home Typing Data Entry Partnerships
By arturmoniswork in forum Reviews / AdvertisingReplies: 0Last Post: 12-30-2008, 05:55 AM -
Global Data Entry. Earn $2,000 a day use rebate lower price save money $$$
By arturmoniswork in forum Reviews / AdvertisingReplies: 0Last Post: 12-27-2008, 01:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks