Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I'm trying to use the JOptionPane here to send a warning to the user that he needs to enter a valid input in correct format, but the JOptionPane is not showing when I entered a wrong input. I don't know what is my mistake.

Java
boolean format = false;
do {

      System.out.print("Enter time in: ");
      in = get.next();
      try {

          LocalTime time = LocalTime.parse(in);
          DateTimeFormatter pattern = DateTimeFormatter.ofPattern("hh:mm a");
          System.out.println(time.format(pattern));
          format = true;
      } catch (Exception e) {
          format = false;
          JOptionPane.showMessageDialog(null, "Input invalid",
                          "Error", JOptionPane.ERROR_MESSAGE);
      }

}while(!format);


What I have tried:

I tried to put the JOptionPane above the try-catch and it's showing normally, but if I put it in the catch block it's not showing.
Posted
Comments
Richard MacCutchan 11-Dec-20 6:35am    
You cannot use a GUI control in a console application.
lelouch_vi 2 11-Dec-20 9:25am    
I see. I thought my machine has a problem displaying a GUI. thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900