Two beginners mistakes...need some input.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iceman4022
    New Member
    • Feb 2007
    • 5

    #1

    Two beginners mistakes...need some input.

    Here is a program I have to write for an assignment. I cannot get it to exit when I click cancel and for some reason I get an error when you enter 3 as your option. I am sure it is a beginners mistake but I am stuck. Any assistance would be greatly appreciated. Just be easy...I am still new. Thanks.

    *************** *************** *************** *************** *********
    Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    public class MyType1
    {
    	public static void main (String[] args)
    	{
    		//Declaring Class Variables
    		String strChoice, strTryString, strTryInt, strTryDouble;
    		int choice, tryInt;
    		double tryDouble;
    		boolean done = false;
    			while (!done)
    			//while (!done= null)
    			{
    				strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
    				strTryDouble = (null);
    				//strTryDouble = dataIn.readLine();
    			try
    			{
    				choice = Integer.parseInt (strChoice);
    
    				//if (choice == null) finish ();
    				if (choice == 4) exit ();
    
    				switch (choice)
    				{
    					case 1:
    					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
    					break;
    
    					case 2:
    					tryInt = Integer.parseInt (strChoice);
    					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
    					break;
    
    					case 3:
    					tryDouble = Double.parseDouble (strTryDouble);
    					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
    					break;
    
    					case 4:
    
    
    					default:
    					throw new NumberFormatException();
    					//break;
    				}
    			}
    			catch (NumberFormatException e)
    			{
    				JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
    			}
    			}
    			exit ();
    	}
    	public static void exit()
    	{
    		System.exit(0);
    	}
    }
    Last edited by horace1; Feb 11 '07, 09:13 AM. Reason: added code tags
  • Cheez It
    New Member
    • Dec 2006
    • 4

    #2
    showInputDialog () returns null when you click cancel so just check if strChoice is null before continuing. You also need to call exit() in case 4. The way you have it now because there is no break in case 4 it continues on to the default case and displays the "Wrong!!", "WAAAY OFF!!!" message. You get a NullPointerExce ption when you enter 3 because you set strTryDouble to null then tried to parse a double from it. I'm not sure exactly what you're trying to do but I think you want to parse strChoice instead of strTryDouble on line 34.

    Comment

    • iceman4022
      New Member
      • Feb 2007
      • 5

      #3
      Sorry if I sould inexperienced on this but to be completely honest...I am. Now I know you said I need to place exit() in case 4 but when you run the program...if you type in 4 it ends the program so I am pretty sure that part is fine...I just need to add an exit message as I have done in my recent code but it errors out. But again if I click cancel...it just displays the "Wrong!!", "WAAAY OFF!!!" message and starts back over. Also as for the

      tryDouble = Double.parseDou ble (strTryDouble);

      I would think that you would just parse it to strChoice but the assignment says I have to Double.parseDou ble it to strTryDouble and I cannot figure out how to get it to work. Thanks for the help so far but any further suggestions? The recent code is as follows:

      *************** *************** *************** *************** *************** *************** **

      Code:
      import java.io.*;
      import javax.swing.JOptionPane;
      import java.text.DecimalFormat;
      
      public class MyType1
      {
      	public static void main (String[] args)
      	{
      		//Declaring Class Variables
      		String strChoice, strTryString, strTryInt, strTryDouble;
      		int choice, tryInt;
      		double tryDouble;
      		boolean done = false;
      			while (!done)
      			//while (!done= null)
      			{
      				strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
      				strTryDouble = (null);
      			try
      			{
      				choice = Integer.parseInt (strChoice);
      
      				//if (choice == null) finish ();
      				if (choice == 4) exit ();
      
      				switch (choice)
      				{
      					case 1:
      					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
      					break;
      
      					case 2:
      					tryInt = Integer.parseInt (strChoice);
      					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
      					break;
      
      					case 3:
      					tryDouble = Double.parseDouble (strTryDouble);
      					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
      					break;
      
      					case 4:
      					JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.", JOptionPane.INFORMATION_MESSAGE);
      					break;
      
      
      					default:
      					throw new NumberFormatException();
      					//break;
      				}
      			//throw new NumberFormatException();
      			}
      			catch (NumberFormatException e)
      			{
      				JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
      			}
      			}
      			exit ();
      	}
      	public static void exit()
      
      	{
      		System.exit(0);
      	}
      } //end

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by iceman4022
        Sorry if I sould inexperienced on this but to be completely honest...I am. Now I know you said I need to place exit() in case 4 but when you run the program...if you type in 4 it ends the program so I am pretty sure that part is fine...I just need to add an exit message as I have done in my recent code but it errors out. But again if I click cancel...it just displays the "Wrong!!", "WAAAY OFF!!!" message and starts back over. Also as for the

        tryDouble = Double.parseDou ble (strTryDouble);

        I would think that you would just parse it to strChoice but the assignment says I have to Double.parseDou ble it to strTryDouble and I cannot figure out how to get it to work. Thanks for the help so far but any further suggestions? The recent code is as follows:

        *************** *************** *************** *************** *************** *************** **

        Code:
        import java.io.*;
        import javax.swing.JOptionPane;
        import java.text.DecimalFormat;
         
        public class MyType1
        {
        	public static void main (String[] args)
        	{
        		//Declaring Class Variables
        		String strChoice, strTryString, strTryInt, strTryDouble;
        		int choice, tryInt;
        		double tryDouble;
        		boolean done = false;
        			while (!done)
        			//while (!done= null)
        			{
        				strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
        				strTryDouble = (null);
        			try
        			{
        				choice = Integer.parseInt (strChoice);
         
        				//if (choice == null) finish ();
        				if (choice == 4) exit ();
         
        				switch (choice)
        				{
        					case 1:
        					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
        					break;
         
        					case 2:
        					tryInt = Integer.parseInt (strChoice);
        					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
        					break;
         
        					case 3:
        					tryDouble = Double.parseDouble (strTryDouble);
        					JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
        					break;
         
        					case 4:
        					JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.", JOptionPane.INFORMATION_MESSAGE);
        					break;
         
         
        					default:
        					throw new NumberFormatException();
        					//break;
        				}
        			//throw new NumberFormatException();
        			}
        			catch (NumberFormatException e)
        			{
        				JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
        			}
        			}
        			exit ();
        	}
        	public static void exit()
         
        	{
        		System.exit(0);
        	}
        } //end
        What is this program supposed to do?

        Comment

        • iceman4022
          New Member
          • Feb 2007
          • 5

          #5
          Originally posted by r035198x
          What is this program supposed to do?
          It is supposed to open a window with four options. You are supposed to enter a selection 1-4. Upon entering the selections 1-3, you will receive the "CORRECT" response. If you enter anything besides 1-4, you will get the "WRONG" response.

          The problem I am running into is the double parsing of case 3. The assignment states I have to parse it to strTryDouble. I am also having a problem with the cancel button. If I click the close "X" or the cancel button, I get the "WRONG" response and it just continues until I enter 4.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by iceman4022
            It is supposed to open a window with four options. You are supposed to enter a selection 1-4. Upon entering the selections 1-3, you will receive the "CORRECT" response. If you enter anything besides 1-4, you will get the "WRONG" response.

            The problem I am running into is the double parsing of case 3. The assignment states I have to parse it to strTryDouble. I am also having a problem with the cancel button. If I click the close "X" or the cancel button, I get the "WRONG" response and it just continues until I enter 4.
            Your explanation does not match what you are doing there. According to you explanation you only need to di Integer.parseIn t fo the option entered once. But in your code you are also doing Double.parseDou ble for case 3 and another Integer.parseIn t for case 2 of the switch. What are those for?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              I strongly suspect, however, that you want the user to be able to enter some input and you test if the input's type matches the previously selected type. Something like this

              Code:
               
              
              import java.io.*;
              import javax.swing.JOptionPane;
              import java.text.DecimalFormat;
              
              public class MyType1 {
               public static void main (String[] args) {
                //Declaring Class Variables
                boolean done = false;
                String strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
                if(strChoice == null || strChoice.equals("4")) {
                 done = true;
                }
                while (!done) {
                 try {
              	int choice = Integer.parseInt (strChoice);
              	switch (choice) {
              	 case 1: {
              	  String tryString = JOptionPane.showInputDialog (null,"Enter a String");
              	  JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
              	  break;
              	 }
              	 case 2: {
              	  String tryStrInt = JOptionPane.showInputDialog (null,"Enter an integer");
              	  int tryInt = Integer.parseInt(tryStrInt);
              	  JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
              	  break;
              	 }
              	 case 3: {
              	  String tryStrDouble = JOptionPane.showInputDialog (null,"Enter a double");
              	  double tryDouble = Double.parseDouble(tryStrDouble);
              	  JOptionPane.showMessageDialog (null, "Correct!!", "THAT’S CORRECT!!!", JOptionPane.INFORMATION_MESSAGE);
              	  break;
              	 }
              	 case 4: {
              	  JOptionPane.showMessageDialog (null, "Thanks for playing...have a nice day.");
              	  break;
              	 }
              	 default: throw new NumberFormatException();
              
              	}
                 }
                 catch (NumberFormatException e) {
              	JOptionPane.showMessageDialog (null, "Wrong!!", "WAAAY OFF!!!", JOptionPane.INFORMATION_MESSAGE);
                 }
                 strChoice = JOptionPane.showInputDialog (null,"What's My Type?\n\n1 ) String\n2 ) integer\n3 ) double\n4 ) Quit the program");
                 if(strChoice == null || strChoice.equals("4")) {
              	done = true;
                 }
                }
               }
              }

              Comment

              • iceman4022
                New Member
                • Feb 2007
                • 5

                #8
                Thanks r035198x...that may do it...I need to check it when I get home where I have a compiler. I know there are easier ways to do thsi but the assignment wants me to use these specific methods. Anyway...I will let you know how it turns out. Thanks again.

                -ICEMAN

                Comment

                • iceman4022
                  New Member
                  • Feb 2007
                  • 5

                  #9
                  Got it...thanks for the help...you definitely gave me some ideas.

                  -ICEMAN

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    You are welcome anytime.

                    Comment

                    Working...