I tried the following code combining BufferedReader/InputStreamReader,
JOptionPane methods and switch concepts. The program compiles fines and no
runtime errors too. But would not execute as expected.
For input '1', I am getting the output 'ONE" as expected. But for other
cases, I am not getting the output in JOptionPane Dialog. If I update the
code to get input through JOptionPane showInputDialog method instead of
InputStreamReader readLine method, the program is executing fine.
Is there any rule that showInputDialog method must be used for
showMessageDialog method to work ? Not sure what am I doing wrong here.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import javax.swing.JOptionPane;
public class SwitchTest {
/**
* @param args
*/
public static void main(String[] args) {
BufferedReader dataIn = new BufferedReader (new
InputStreamReader(System.in));
try {
System.out.println("Enter a number from 1 to 5:");
int dataInIntVal = Integer.parseInt(dataIn.readLine());
switch (dataInIntVal) {
case 1:
System.out.println("ONE");
break;
case 2:
JOptionPane.showMessageDialog(null, "TWO");
break;
case 3:
JOptionPane.showMessageDialog(null, "THREE");
break;
case 4:
JOptionPane.showMessageDialog(null, "FOUR");
break;
case 5:
JOptionPane.showMessageDialog(null, "FIVE");
break;
default:
JOptionPane.showMessageDialog(null, "You entered a different number");
break;
} //switch ends
} //try ends
//Handle IOException for readLine() method or NumberFormatException for
parseInt method
catch ( IOException e) {
System.out.println("ERROR! Invalid data.");
}
} //main method ends
}
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/jpassion_java?hl=en-US.