Hey all,
I am working on an assignment and need to do the following, but the book really doesn't give a good explanation on how to do it. Any help would be much appreciated.
Case 1: If the user enters a 1, display a message that informs users they are correct, as any input can be saved as a
String.
Case 2: If the user enters a 2, parse the value into tryInt. Display a message that informs users they are correct.
Case 3: If the user enters a 3, parse the value into tryDouble. Display a message that informs the user they are correct.
Case 4: Set done equal to true.
Here is what my code looks like right now. I would attach it as a zip file, but it looks like I can't.
/*
Chapter 4: Switch and Try Statements
Programmer: Justin Mosley
Date: Sept 12, 2007
Filename: MyType.java
Purpose: This program helps beginning programmers understand data types.
*/
import java.io.*;
import javax.swing.JOptionPane;
public class MyType
{
public static void main(String[] args)
{
//declare class variables
String strChoice, strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
}
//loop while not done
while (!done)
{
try
{
String message = "What's My Type?" + "\n\n1)String\n2)integer\n3)double\n4)Quit the program\n\n";
choice = Integer.parseInt(strChoice);
//test for valid codes 1, 2, 3, or 4
if (choice<1 || code>4) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter a 1, 2, 3, or 4", "Error", JOptionPane.INFORMATION_MESSAGE);
}
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
}
}