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
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
Comment