469,590 Members | 2,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,590 developers. It's quick & easy.

due in 45 mins help! check input for double or integer

Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then display a "correct" message if is correct.
So, If you choose 2 from the original pane, you would be prompted to enter an integer. The program would check to see if you input an integer and if it was then you would get a "correct" message. If not you would get an error message.
I have tried and tried to get this and I am out of time here. It does not go over this in my book and I have only had Java a few weeks now. PLEASE HELP if you can. I really really appreciate it!
Thanks

Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.JOptionPane;
  3. import java.io.*;
  4.  
  5. public class MyTypeafter
  6. {
  7.     public static void main(String[] args) throws IOException
  8.     {
  9.         // declare and construct variables
  10.         String strChoice, strTryString, strTryInt, strTryDouble;
  11.         int choice, tryInt;
  12.         double tryDouble;
  13.         boolean done = false;
  14.  
  15.         // loop while user does not click cancel button
  16.         while(!done)
  17.         {
  18.         try
  19.             {
  20.                 String myMsg = "What's my Type?\n";
  21.                 myMsg = myMsg + "1) String\n";
  22.                 myMsg += "2) Integer\n";
  23.                 myMsg += "3) Double\n";
  24.                 myMsg += "4) Quit the Program\n";
  25.  
  26.                 strChoice = JOptionPane.showInputDialog(null, myMsg, "Input", JOptionPane.QUESTION_MESSAGE);
  27.                 if(strChoice == null) break;
  28.                 choice = Integer.parseInt(strChoice);
  29.  
  30.             switch(choice)
  31.                 {
  32.                 case 1:
  33.                     JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
  34.                     break;
  35.  
  36.                 case 2:
  37.                     JOptionPane.showMessageDialog(null, "Please enter an integer");
  38.                     tryInt = Integer.parseInt(strChoice);
  39.                     if (strChoice == integer)
  40.                     {
  41.                         JOptionPane.showMessageDialog("correct");
  42.                     }//end if
  43.                     break;
  44.  
  45.                 case 3:
  46.                     JOptionPane.showMessageDialog(null, "Correct Double Value");
  47.                     tryDouble = Double.parseDouble(strChoice);
  48.                     break;
  49.  
  50.                 case 4:
  51.                     done = true;
  52.                     JOptionPane.showMessageDialog(null, "Now Closing");
  53.                     break;
  54.  
  55.                 default:
  56.                 throw new NumberFormatException();
  57.  
  58.                 }//end switch
  59.  
  60.               }//end try
  61.  
  62.         catch(NumberFormatException e)
  63.             {
  64.             String myMsg2 = "Error, problem with number format\n" + e;
  65.             JOptionPane.showMessageDialog(null, myMsg2, "Error", JOptionPane.INFORMATION_MESSAGE);
  66.             }//end catch
  67.  
  68.         }//end while
  69.  
  70.     }//end main method
  71.  
  72. }//end class
  73.  
  74.  
Oct 18 '06 #1
1 7191
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  
  3. public class MyType
  4. {
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         // declare and construct variables
  9.  
  10.  
  11.  
  12.         int choice = 0;
  13.         int tryInt = 0;
  14.         double tryDouble = 0.0;
  15.         boolean done = false;
  16.  
  17.         // loop while user does not click cancel button
  18.         String entered = "";
  19.         while(!done) {
  20.             try    {
  21.                 String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
  22.                 if(strChoice == null) {
  23.                     strChoice = ""+4;
  24.                 }
  25.                 choice = Integer.parseInt(strChoice);
  26.                 switch(choice) {
  27.                     case 1: {
  28.                         entered = JOptionPane.showInputDialog(null,"Enter The String\n");
  29.                         JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
  30.                         break;
  31.                     }
  32.                     case 2: {
  33.                         entered = JOptionPane.showInputDialog(null,"Enter The integer\n");
  34.                         tryInt = Integer.parseInt(entered);
  35.                         JOptionPane.showMessageDialog(null, "Correct");
  36.                         break;
  37.                     }
  38.                     case 3: {
  39.                         entered = JOptionPane.showInputDialog(null,"Enter The Double\n");
  40.                         tryDouble = Integer.parseInt(entered);
  41.                         JOptionPane.showMessageDialog(null, "Correct");
  42.  
  43.                         break;
  44.                     }
  45.                     case 4:
  46.                         done = true;
  47.                         JOptionPane.showMessageDialog(null, "Now Closing");
  48.                         break;
  49.                     default:
  50.                         throw new NumberFormatException();
  51.  
  52.                 }
  53.             }
  54.             catch(NumberFormatException e) {
  55.                 JOptionPane.showMessageDialog(null, "Please try again");
  56.             }
  57.         }
  58.  
  59.         System.exit(0);
  60.  
  61.     }
  62.  
  63. }
oops hope this is not too late!
Oct 18 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by wukexin | last post: by
7 posts views Thread by Corepaul | last post: by
1 post views Thread by switzerland qunatium computer | last post: by
8 posts views Thread by Mark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.