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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redpayne
    New Member
    • Oct 2006
    • 5

    #1

    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

    Code:
    import javax.swing.JOptionPane;
    import java.io.*;
    
    public class MyTypeafter
    {
    	public static void main(String[] args) throws IOException
    	{
    		// declare and construct variables
    		String strChoice, strTryString, strTryInt, strTryDouble;
    		int choice, tryInt;
    		double tryDouble;
    		boolean done = false;
    
    		// loop while user does not click cancel button
    		while(!done)
    		{
    		try
    			{
    				String myMsg = "What's my Type?\n";
    				myMsg = myMsg + "1) String\n";
    				myMsg += "2) Integer\n";
    				myMsg += "3) Double\n";
    				myMsg += "4) Quit the Program\n";
    
    				strChoice = JOptionPane.showInputDialog(null, myMsg, "Input", JOptionPane.QUESTION_MESSAGE);
    				if(strChoice == null) break;
    				choice = Integer.parseInt(strChoice);
    
    			switch(choice)
    				{
    				case 1:
    					JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
    					break;
    
    				case 2:
    					JOptionPane.showMessageDialog(null, "Please enter an integer");
    					tryInt = Integer.parseInt(strChoice);
    					if (strChoice == integer)
    					{
    						JOptionPane.showMessageDialog("correct");
    					}//end if
    					break;
    
    				case 3:
    					JOptionPane.showMessageDialog(null, "Correct Double Value");
    					tryDouble = Double.parseDouble(strChoice);
    					break;
    
    				case 4:
    					done = true;
    					JOptionPane.showMessageDialog(null, "Now Closing");
    					break;
    
    				default:
    				throw new NumberFormatException();
    
    				}//end switch
    
    			  }//end try
    
    		catch(NumberFormatException e)
    			{
    			String myMsg2 = "Error, problem with number format\n" + e;
    			JOptionPane.showMessageDialog(null, myMsg2, "Error", JOptionPane.INFORMATION_MESSAGE);
    			}//end catch
    
    		}//end while
    
    	}//end main method
    
    }//end class
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Code:
    import javax.swing.JOptionPane;
    
    public class MyType
    {
    
    	public static void main(String[] args)
    	{
    		// declare and construct variables
    
    
    
    		int choice = 0;
    		int tryInt = 0;
    		double tryDouble = 0.0;
    		boolean done = false;
    
    		// loop while user does not click cancel button
    		String entered = "";
    		while(!done) {
    			try	{
    				String strChoice = JOptionPane.showInputDialog(null,"What's My Type?\n1)String\n2)integer\n3)double\n4Quit the program\n");
    				if(strChoice == null) {
    					strChoice = ""+4;
    				}
    				choice = Integer.parseInt(strChoice);
    				switch(choice) {
    					case 1: {
    						entered = JOptionPane.showInputDialog(null,"Enter The String\n");
    						JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String");
    						break;
    					}
    					case 2: {
    						entered = JOptionPane.showInputDialog(null,"Enter The integer\n");
    						tryInt = Integer.parseInt(entered);
    						JOptionPane.showMessageDialog(null, "Correct");
    						break;
    					}
    					case 3: {
    						entered = JOptionPane.showInputDialog(null,"Enter The Double\n");
    						tryDouble = Integer.parseInt(entered);
    						JOptionPane.showMessageDialog(null, "Correct");
    
    						break;
    					}
    					case 4:
    						done = true;
    						JOptionPane.showMessageDialog(null, "Now Closing");
    						break;
    					default:
    						throw new NumberFormatException();
    
    				}
    			}
    			catch(NumberFormatException e) {
    				JOptionPane.showMessageDialog(null, "Please try again");
    			}
    		}
    
    		System.exit(0);
    
    	}
    
    }
    oops hope this is not too late!

    Comment

    Working...