aspose file tools*
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
Author

InputStreamReader and JOptionPane related issue

mahesh rao
Greenhorn

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 java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import javax.swing.JOptionPane;

public class SwitchTest {

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

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

} //main method ends
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
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
Greenhorn

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
Greenhorn

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: InputStreamReader and JOptionPane related issue