Results 1 to 12 of 12
  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default 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.

    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);
    	
    
    	}
    
    }
    Thanks !

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

    Default

    Quote Originally Posted by aconti View Post
    I am having an error.
    We don't read minds. Post the full and exact error message.

  3. #3
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    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)

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

    Default

    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"?

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    "" 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 null
    Last edited by mine0926; 06-15-2011 at 09:04 AM.

  6. #6
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    thanks but how do I parse only if upd is not equals to ""

  7. #7
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    ok found out, just copied the parse section in the else section inside the if condition as it was out !

  8. #8
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    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 ?

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

    Default

    Quote Originally Posted by mine0926 View Post
    In this case, upd is null
    Not it isn't. The error message clearly says that the invalid input is an empty String. Besides JOptionPane will never return null.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    Not it isn't. The error message clearly says that the invalid input is an empty String. Besides JOptionPane will never return null.
    Yes it can.
    Java Code:
    System.out.println(JOptionPane.showInputDialog(null,
            "Close this dialog using the [x] button in the title bar") == null);
    db

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

    Default

    I cry foul. Well I was talking about getting user input. Interesting it also returns null if the Cancel button is clicked.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    As documented, but unfortunately only for one of the many overloads.

    db
    Last edited by DarrylBurke; 06-16-2011 at 09:30 AM. Reason: Danged link parser doesn't understand []

Similar Threads

  1. Replies: 10
    Last Post: 02-11-2011, 08:15 PM
  2. Data Entry Workers Needed Worldwide
    By rajshree in forum Jobs Offered
    Replies: 0
    Last Post: 01-30-2011, 06:10 AM
  3. Save data entry
    By tiger100plus in forum NetBeans
    Replies: 2
    Last Post: 12-08-2009, 02:53 PM
  4. Make money from home, Home Typing Data Entry Partnerships
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 05:55 AM
  5. Global Data Entry. Earn $2,000 a day use rebate lower price save money $$$
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-27-2008, 01:35 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
  •