Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InputStreamReader and JOptionPane related issue

 
mahesh rao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48409
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked fine for me in Eclipse with Java 7 u7. The dialog popped up underneath all other windows.
 
mahesh rao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic