Results 1 to 15 of 15
  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Java Error Check

    I have a JOptionPane that displays and asks the user to input a number. I only want this input to accept ODD numbers. How would I check to make sure that the number entered is ODD only? I have no idea even where to begin on this one?!

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    Where do you want the validation to occur? While they type? When they hit enter?

    First, we have to know if a number is odd. How do you know if a number is odd?

    Second, how do you get input from a JOptionPane?

    Third, how do you use that input? What kind of checks would you perform on it to see if it's odd?

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

    Default Re: Java Error Check

    I want the validation to occur once the user hits enter.

    To check if a number is odd (I THINK....) I could use
    Java Code:
    if (var1 % 2 != 0)
    //Then the number is odd
    The way I was going to get input from a JOption Pane was going to be:
    Java Code:
    var1= Integer.valueOf(JOptionPane.showInputDialog("Enter an odd number"));
    If the number is not odd, I want display an error message (I know I would use a do-while loop for this), if the number is odd, I want to pass var1 to a different module, and continue with the code.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    Sounds like you have everything you need. What's the issue then?

  5. #5
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Error Check

    My java compiler doesn't like the "%" sign?

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    Post your code. Use code tags.

  7. #7
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Error Check

    This is the statement I am working with (or trying to make work)
    Java Code:
    	{
    	String var1;
    	
    		do{var1 = Integer.valueOf(JOptionPane.showInputDialog("Enter an odd number"));
    			if (var1 % 2 == 0){
    				JOptionPane.showMessageDialog(null, "Please enter an odd number!");
    				}else{
    					Advance(var1);
    			}
    			}while (var1 % 2 != 0);
        }

  8. #8
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    Are you sure var1 is an integer? What's the remainder of a String?

  9. #9
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Error Check

    I could be totally wrong in saying this, but I thought that input into the JOptionPane had to be input as a string and then converted to what type you actually needed it

  10. #10
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    Quote Originally Posted by Rahim2312 View Post
    Java Code:
    	{
    	String var1;
    		var1 = Integer.valueOf(JOptionPane.showInputDialog("Enter an odd number"));
    You're converting from a String to an int to a String again. Declare var1 as an int.

    Your input is a String - "Enter an odd number"
    Your output is also a String, which you've converted to an int. You need to store it in an int as well.

  11. #11
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Error Check

    Ah Hah! One more issue I am running into...with the code from my post 7. If an even number is entered, it is giving me the message to "Enter an odd number" like it should, but then it is leaving the program, I want it to loop back up and ask me to again input an odd number.

  12. #12
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    }while (var1 % 2 != 0);

    Does this say "do the above while var1 is even"? Or does it say "Do the above while var1 is odd"?

  13. #13
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Error Check

    It says to do the above code while var1 is odd. Which means I should flip it to read:
    Java Code:
    var1 % 2 == 0
    Last edited by Rahim2312; 05-08-2012 at 09:24 PM.

  14. #14
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Java Error Check

    What is the error case? Should it repeat the question if var1 is odd?

  15. #15
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Error Check

    Switching the symbol like below fixed it. If the number was odd, it should have carried on to Advance. Changing the code to:
    Java Code:
    var1 %2 == 0
    has it functioning as needed.

    Thank you for your time and support.

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2011, 08:37 PM
  2. Pls check why my code is giving a runtime error
    By Beginner in forum New To Java
    Replies: 3
    Last Post: 05-26-2010, 12:32 AM
  3. Replies: 2
    Last Post: 08-31-2009, 02:50 PM
  4. how to check java
    By funkygarzon in forum New To Java
    Replies: 10
    Last Post: 03-08-2009, 08:39 AM
  5. CRC check program 1 error
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 05:04 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
  •