JOptionPane.showMessageDialog is not showing

2019-09-16 07:34发布

问题:

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?