Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a more efficient and perfect solution to your problem:</p> <pre><code>import java.io.*; import javax.swing.JOptionPane; public class MyType { public static void main(String[] args) { // declare class variables String strChoice=""; // you didn't need the other variables Integer 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"); if(strChoice.equals("")) // refuses blank answers, must enter a choice { JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); continue; } choice = Integer.parseInt(strChoice); if(choice !=4) // so the input dialog doesn't appear when choice is 4 strChoice = 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(strChoice); JOptionPane.showMessageDialog(null, "Correct."); break; case 3: tryDouble = Double.parseDouble(strChoice); JOptionPane.showMessageDialog(null, "Correct."); break; case 4: done = true; JOptionPane.showMessageDialog(null, "The program will now close."); break; default: { if((choice &gt; 4) || (choice == null)) // must be 1 - 4, &amp; not blank JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.","Error",JOptionPane.INFORMATION_MESSAGE); } } } catch(NumberFormatException e) // this should ONLY occur when type is wrong { JOptionPane.showMessageDialog(null,"Your input can't be stored into that type.","Error",JOptionPane.INFORMATION_MESSAGE); } } } } </code></pre> <p>Also, in the cases, you are supposed to parse <code>strChoice</code>, not <code>choice</code>. <code>choice</code> is was already converted to an int before, so it wouldn't be a good comparison. Using <code>strString</code> truly compares the user input against the type. </p> <p>I also changed <code>int</code> to <code>Integer</code>, <code>int</code> is a primitive data type and can't be checked for <code>null</code> values. <code>Integer</code> can do all that <code>int</code> can, but it's an object, and objects can be null. If choice happens to be <code>null</code> we can check for it and do something about it.</p> <p>While the answer you accepted is not wrong, the code is too repetitive (not a good programming practice), and is a great departure from your original code. There were exceptions when the 'error' message didn't fit the description of what really was wrong. Also, the program got a lot more complex: in programming simpler is better.</p> <p>Hope this helps! Let me know if you have any questions!</p> <p>Cheers! :)</p> <p>JLL </p>
    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.
    1. VO
      singulars
      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