Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Exceptions and JOptionPane Cancel button

807598Oct 24 2006 — edited Oct 24 2006
Hi Guys, Sorry to bother you all, but I am hoping for some assistance in fixing this code. The project is from my "Programming in Java" course, and its to work with "Try and Switch" statements. I believe there are errors in the logic that force NumberFormatExceptions (on purpose) but I am not sure. Also I cannot seem to get the cancel button to work correctly. Its already throwing a "null" value, as verified by System.out.print statements, but the Cancel button is throwing aNumberFormatException. I am so confused right now. Here is the code ...
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{//Class Open

	public static void main(String[] args)//throws IOException
	{//Main Open
	
		//Delcare variables
		String strChoice, strTryString, strTryInt, strTryDouble, strTryLong, strTryByte, strTryBoolean;
		int choice, tryInt;
		double tryDouble;
		
		long tryLong; //extra credit
		byte tryByte; //extra credit
		boolean tryBoolean; //extra credit
		
		boolean done = false;
								
		//loop while not done until user clicks the cancel button
		while(!done)
		{//While Open
		
			try //inside a try statement, enter code to display and input box with 3 choices
			{//Try Open
			
				strChoice =(JOptionPane.showInputDialog(null, "What is My Type?" + "\n\n1) String\n2) Integer\n3) Double\n4) Long\n5) Byte\n6) Boolean\n7) Quit the program\n\n"));
			
				choice = Integer.parseInt(strChoice);
				
				// Error Messages inform me of Strings not initialised 
				strChoice = null; 
				strTryInt = null; 
				strTryDouble = null;
				strTryLong = null;
				strTryByte = null;
				strTryBoolean = null;
								
				if (choice == 0) System.exit(0);
				
				System.out.println(strChoice);
				System.out.println(strTryDouble);
				System.out.println(strTryInt);
				System.out.println(strTryLong);
				System.out.println(strTryBoolean);
				
			switch(choice)
			{//Switch Open
				case 1://If Users enter a 1, display "correct" message 
					JOptionPane.showMessageDialog(null, "Very Nice! That is Correct, Because any input can be saved as a String!");
					break;
					
				case 2://If User enters a 2, parse the value into tryInt, Inform users they are correct
					tryInt = Integer.parseInt(strTryInt);
								System.out.println(choice);
								JOptionPane.showMessageDialog(null, "You are Correct!");
					break;
					
				case 3://If User enters a 3, parse value into tryDouble, Inform users they are correct
					
					tryDouble = Double.parseDouble(strTryDouble +.1);
								System.out.print(choice); 
								JOptionPane.showMessageDialog(null,  "That is Correct!");
					break;
				case 4://extra credit Long
					tryLong = Long.parseLong(strTryLong);
					JOptionPane.showMessageDialog(null, "That is Correct!");
					break;
				
				case 5: //extra credit Byte 
					tryByte = Byte.parseByte(strTryByte);
					JOptionPane.showMessageDialog(null, "That is Correct!");
					break;
					
				case 6: //extra credit Boolean 
					tryBoolean = Boolean.valueOf(strTryBoolean);
					JOptionPane.showMessageDialog(null, "That is Correct!");
					break;
					
				case 7://set done equal to true, code closing message
					done = true;
								JOptionPane.showMessageDialog(null, "Thanks for trying MyType");
					break;
									
				default: throw new NumberFormatException(); 
				
								
			}//Switch Close
				//if(strChoice == null) System.exit(0);
				
			}//Try Close
			
			catch(NumberFormatException e)
			
			{//Catch Open
				JOptionPane.showMessageDialog(null,"OOOPS! try again", "Not a String Error", JOptionPane.INFORMATION_MESSAGE);
				
			}//Catch Close
			
		}//While Close

		
		//
		
	}//Main Close
	
				
		
	
	
	
}//Class Close
Any assistance would be greatly appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2006
Added on Oct 24 2006
2 comments
2,035 views