Note that there are some explanatory texts on larger screens.

plurals
  1. POI have unused variables and my code always throws an exception upon validation, regardless if valid or not
    primarykey
    data
    text
    <p>Good evening,</p> <p>I'm doing an assignment for an introductory java class that requires me to use switch and try statements to validate user input. The user of the java application should enter a number associated with a data type (1 - string, 2 - integer, 3 - double, or 4 - quit). That number should be validated, but the validation process always throws and exception no matter if the data is valid or not. After doing this, this user is supposed to be able to enter data and have the application validate that said entered data is indeed of the selected data type. Given I keep running into a thrown exception, I haven't even been able to test this portion of the code, but am certain it won't work given I see three unused variables that, according to my text, should be used (strTryString, strTryInt, and strTryDouble). I can't make the connection mentally of how, upon initial selection of a data type, the program will then say "user selected double, compare input[strTryDouble?] to parameters of double data type". Any help with this would be greatly appreciated. Here is what I have thus far:</p> <pre><code>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 { strChoice = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program"); choice = Integer.parseInt(JOptionPane.showInputDialog(null,strChoice)); switch(choice) { case 1: JOptionPane.showMessageDialog(null, "Correct, any input can be saved as a String."); break; case 2: tryInt = Integer.parseInt(strTryInt); JOptionPane.showMessageDialog(null, "Correct."); break; case 3: tryDouble = Double.parseDouble(strTryDouble); JOptionPane.showMessageDialog(null, "Correct."); break; case 4: done = true; JOptionPane.showMessageDialog(null, "The program will now close."); break; default: throw new NumberFormatException(); } } catch(NumberFormatException e) { JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload