Results 1 to 5 of 5

Thread: [RESOLVED] Help with JOptionPane

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Resolved [RESOLVED] Help with JOptionPane

    I'm in the US Marines and stationed in Japan. I'm taking an online Java Programming class (part of my degree curriculum) and am only in week 2. I have the following task:

    Write a Java program that reads an integer and checks whether it is even.

    If your input is 25, your output should be:

    Is 25 even?

    false

    If your input is 2000, your output should be:

    Is 2000 even?

    true

    Also, the program must check if the number the user entered is between 1 and 1000.

    If your input is 25, your output should be:

    The number 25 between 1 and 1000 is true.

    If your input is 2000, your output should be:

    The number 2000 between 1 and 1000 is false.

    1.The input and output must use JOptionPane dialog and display boxes.
    My code thus far is:

    Code:
    import javax.swing.JOptionPane;
    
    public class NumberChecker {
       public static void main (String args[]) {
          // Prompt the user to enter a number
         String numberString = JOptionPane.showInputDialog(null, "Enter a number:", "Number Checker", JOptionPane.QUESTION_MESSAGE);
    		
         //  Convert the string into an int value
         int number = Integer.parseInt(numberString);
    				
         //  Check if the number is even
         int isEven; { 
         number = number % 2;
    		
         //  Display the result in a message dialog box declaring if it is even or odd
         JOptionPane.showMessageDialog(null, "Is " + number + " an even number?\n> " + isEven, "Number Checker", JOptionPane.INFORMATION_MESSAGE);}
    	}
         //  Check if the number is in the prescribed range
         boolean isRange; {  
         return ! ((1 < number) && (number < 1000)); 
    			
         // Display the result in a message dialog box declaring if it falls in the prescribed range
         JOptionPane.showMessageDialog(null, "Is the number " + number + " between 1 and 1000?\n> " + isRange, "Range Checker", JOptionPane.INFORMATION_MESSAGE);}
         {
         System.exit (0);
         }
    }
    My problem is that when I go to compile, I'm receiving errors for the return ! and an error code for my range checker saying that it is looking for a symbol. Can anyone tell me what I'm doing wrong and why?

    Thank you in advance!

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Re: Help with JOptionPane

    I downloaded JCreator LE and am trying to clean it up a little.

    Here's what I've gotten so far:
    Code:
    import javax.swing.JOptionPane;
    
    public class NumberChecker {
    	public static void main (String args[]) {
    		// Prompt the user to enter a number
    		String numberString = JOptionPane.showInputDialog(null, "Enter a number:", "Number Checker", JOptionPane.QUESTION_MESSAGE);
    
    		//  Convert the string into an int value
    		int number = Integer.parseInt(numberString);
    		int number = range;
    
    		//  Check if the number is even
    		int isEven; {
    		number = number % 2;
    
    		//  Display the result in a message dialog box declaring if it is even or odd
    		JOptionPane.showMessageDialog(null, "Is " + number + " an even number?\n> " + isEven, "Number Checker", JOptionPane.INFORMATION_MESSAGE);}
    	}
    		//  Check if the number is in the prescribed range
    		boolean isRange; {
    			return ! ((1 < range) && (range < 1000));
    
    		// Display the result in a message dialog box declaring if it falls in the prescribed range
    		JOptionPane.showMessageDialog(null, "Is the number " + range + " between 1 and 1000?\n> " + isRange, "Range Checker", JOptionPane.INFORMATION_MESSAGE);}
    		{
    		System.exit (0);
    	}
    }
    Still getting the errors, however.

  3. #3
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Help with JOptionPane

    Uhh, I would recommend you go through a basic Java tutorial because this is all wonky.

    You're not declaring your methods properly and you also don't have any error catching.

    Here is a quick example of what you need to do. There are much better ways to do this, but here are the basics.

    Code:
      try{
    		   
    	      // Prompt the user to enter a number
    	     String numberString = JOptionPane.showInputDialog(null, "Enter a number:", "Number Checker", JOptionPane.QUESTION_MESSAGE);
    			
    	     //  Convert the string into an int value
    	     int number = Integer.parseInt(numberString);
    					
    	     //  Check if the number is even
    	     int checkEven = number &#37; 2;
    	     
    	     boolean isEven = false;
    	     
    	     if (checkEven == 0)
    	     	isEven = true;
    	     
    	     boolean inRange = false;
    	     if (number < 1000 && number > 1)
    	    	 inRange = true;
    			
    	     //  Display the result in a message dialog box declaring if it is even or odd
    	     JOptionPane.showMessageDialog(null, "Is " + number + " an even number?\n> " + isEven, "Number Checker", JOptionPane.INFORMATION_MESSAGE);
    	   	 JOptionPane.showMessageDialog(null, "Is the number " + number + " between 1 and 1000?\n> " + inRange, "Range Checker", JOptionPane.INFORMATION_MESSAGE);
    	   	 
    		   }catch(NumberFormatException ne){
    			   JOptionPane.showMessageDialog(null, "You must enter an integer.", "Number Format Error", JOptionPane.ERROR_MESSAGE);
    		   }catch (Exception e){
    			   JOptionPane.showMessageDialog(null, "An Unknown error has occured.","Unknown Error", JOptionPane.ERROR_MESSAGE);
    		   }//end try/catch

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Re: Help with JOptionPane

    The class I'm enrolled in is an Intro to Java, so I guess it's basically a tutorial. The book (JAVA: How to Program 7th ed) presents the material in basic syntax that is run from the command line and has input/output there also. The instructor presented a couple of very basic dialog box examples, but the context is a little outside my reasoning so far.

    I really appreciate your help. I wasn't aware of the the "checkEven" command and that alone makes a world of difference. I pasted what you presented into my original in JCreator and am still getting a few errors with the error checking that you provided; I'll have to iron that out. I'm sure it's probably declarations on my part again.

    Thank you again and have a great day!

  5. #5
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Help with JOptionPane

    Glad to help, but 1 thing: checkEven is a variable, not a method.

    Since you were using input &#37;(mod) 2, if it was even it would return 0. So if it was 0, I was telling the program that the number was even.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width