JOptionPane.showMessageDialog is not showing

隐身守侯 提交于 2019-12-13 04:56:36

问题


So, this is my first time over here and I'm starting with Java this should be a really silly error but I can't find why my program is getting stuck when it enters to the while section. This is a program where I ask name and age, but it cant be a negative number so when this happens I have to show a messageDialog saying that.

package guia1;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Paris
 */
public class LecturaConsola {
public static void main(String args[]){
    Scanner reader = new Scanner(System.in);
    String nombre = "";
    int edad;
    System.out.print("Ingrese su nombre: ");
    nombre=reader.nextLine();
    System.out.print("Ingrese su edad: ");
    edad=reader.nextInt();
    while(edad<0){ //error here
        JOptionPane.showMessageDialog(null, "No se permite ingresar una edad negativa, favor ingresela nuevamente."); 
        System.out.print("Ingrese su edad: ");
        edad=reader.nextInt();
    }
    System.out.println("Nombre: "+nombre);
    System.out.println("Edad: "+edad);
}

}    

When I enter a possitive number it goes all well, but when I enter a negative number the MessageDialog doesn't show and the program gets stuck, any suggestions?

PD. Sorry about my English.

Edit: I'm entering this data,

Paris Forrer

-22

and it gets stuck but if I try,

Paris Forrer

22

works I don't know why my messageDialog is working if I put it before entering any info but not after, is there any chance that I might not have some complement or something like that?

来源:https://stackoverflow.com/questions/28054796/joptionpane-showmessagedialog-is-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!