|
-
April 1st, 2011, 01:55 PM
#1
Program not accepting input?
Here we go again... I never should have taken C++ and Java at the same time. After 5 hours of trying to figure out what's wrong with this program... I'm asking for a second set of eyes. I'm sure its dumb, but please give me a hint as to where my problem is so I can fix it. The sickening part is, the book TELLS you the code. I swear I followed all instructions.... The book never mentions initializing the variables. It LITERALLY says: "Type String strChoice, strTryString, strTryInt, strTryDouble;" ... last time I checked, those need to be initialized. And, curiously enough, it has me declare strTryString, yet never use it.
The problem is, it throws the exception, no matter what I enter, and never lets anything else happen... and I could swear that I have my switch case correct.
PLEASE HELP!
Code:
/*
Chapter 4: Programming Assignment 2
Programmer: Ben ********
Date: 04/01/11
Filename: MyType.java
Purpose:
*/
import java.io.*;
import javax.swing.JOptionPane;
public class MyType
{
public static void main(String[] args)
{
String strChoice = "", strTryString = "", strTryInt = "", strTryDouble = "";
int choice, tryInt;
double tryDouble;
boolean done = false;
while(!done)
{
try
{
JOptionPane.showInputDialog("Ben ******** 04/01/2011\n\n" +
"What's My Type?\n\n" +
"1) String\n" +
"2) Integer\n" +
"3) Double\n" +
"4) Quit the program\n");
choice = Integer.parseInt(strChoice);
switch(choice)
{
case 1:
JOptionPane.showMessageDialog(null,"Ben ******** 04/01/11\n\n" +
"The data type you entered is correct.");
break;
case 2:
tryInt = Integer.parseInt(strTryInt);
JOptionPane.showMessageDialog(null,"Ben ******** 04/01/11\n\n" +
"The data type you entered is correct.");
break;
case 3:
tryDouble = Double.parseDouble(strTryDouble);
JOptionPane.showMessageDialog(null,"Ben ******** 04/01/11\n\n" +
"The data type you entered is correct.");
break;
case 4:
done = true;
break;
default:
throw new NumberFormatException();
}
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Ben ******** 04/01/11\n" +
"You entered an incorrect data type.");
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|