File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes InputStreamReader and JOptionPane related issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "InputStreamReader and JOptionPane related issue" Watch "InputStreamReader and JOptionPane related issue" New topic

InputStreamReader and JOptionPane related issue

mahesh rao

Joined: Sep 08, 2004
Posts: 19
The following program compiled fine and there are no runtime errors too. But it is not working the way I want. Can anyone please help.

When I enter input value as '1', it is displaying the output "ONE" as expected. When I enter any other value, nothing happens.

Is there a rule that JOptionPane showInputDialog method should be used for showMessageDialog to work ?
If I use JOptionPane showInputDialog method instead of InputStreamReader readLine method to get the input, the program starts working fine.
I am not sure why the program is not working properly if I use InputStreamReader readLine method for getting input and JOptionPane showMessageDialog method for displaying output.

import javax.swing.JOptionPane;

public class SwitchTest {

public static void main(String[] args) {

BufferedReader dataIn = new BufferedReader (new InputStreamReader(;

try {
System.out.println("Enter a number from 1 to 5:");
int dataInIntVal = Integer.parseInt(dataIn.readLine());

switch (dataInIntVal) {
case 1:
case 2:
JOptionPane.showMessageDialog(null, "TWO");
case 3:
JOptionPane.showMessageDialog(null, "THREE");
case 4:
JOptionPane.showMessageDialog(null, "FOUR");
case 5:
JOptionPane.showMessageDialog(null, "FIVE");
JOptionPane.showMessageDialog(null, "You entered a different number");
} //switch ends
} //try ends

catch (IOException e) {
System.out.println("ERROR! Invalid data.");

} //main method ends
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
It works all right for me.
Beware of these dialogue windows. They can appear behind other things on the screen, and you might need to minimise other windows before you see the dialogues.
mahesh rao

Joined: Sep 08, 2004
Posts: 19
I tried both in Eclipse and Netbeans. The issue persists. When I enter input other than 1, output is expected in JOptionPane Message Dialog box but nothing is displayed. Not sure if it is an issue with JDK 7.
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
It worked fine for me in Eclipse with Java 7 u7. The dialog popped up underneath all other windows.
mahesh rao

Joined: Sep 08, 2004
Posts: 19
I tested the program again and it worked fine now. As you said, the window may have been hiding underneath all the other windows and I did not notice. Thanks.
I agree. Here's the link:
subject: InputStreamReader and JOptionPane related issue
It's not a secret anymore!