0

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?

Community
  • 1
  • 1
Paris Forrer
  • 3
  • 1
  • 6
  • Not to ask a silly question (this sometimes can be an easy thing to miss) ... by any chance are you debugging this and did you leave a breakpoint at the `JOptionPane.showMessageDialog` line? – chancea Jan 20 '15 at 20:54
  • Seems to work fine for me. Perhaps you should use some `System.out.println("");` statements to dump various values to the console for inspection, then you can see what's going on... – MadProgrammer Jan 20 '15 at 21:09
  • I have already tried your suggestions but it's still not working, when I move the JOptionPane.showMessageDialog line before asking the name, it shows the message but if I move it after entering any data it doesnt work... :/ – Paris Forrer Jan 20 '15 at 23:41
  • what is the exact data you are entering? Include new line characters and all your numbers and strings. Update your question with that information – chancea Jan 21 '15 at 17:38
  • done, is there any chance that my install is corrupt or something? – Paris Forrer Jan 21 '15 at 21:35
  • I guess you could always try a project clean and rebuild – chancea Jan 21 '15 at 22:23
  • Oh wait, perhaps it is that the window is always appearing behind your main window. look at this http://stackoverflow.com/questions/438463/joptionpane-wont-show-its-dialog-on-top-of-other-windows – chancea Jan 21 '15 at 22:29
  • OMG!! it was that, the window was appearing behind all the time!! lol thank you so much! Is there any way I can upvote your answer or something? – Paris Forrer Jan 22 '15 at 00:12

0 Answers0