Search code examples
javaif-statementjoptionpane

If statements not running


The strings were parsed to integers, so the condition of the if statements are properly right set. Why are the if statements not running? Why there doesn't appear a MessageDialog with a response?

 class process{
     public static void whoIs(){

         JFrame frame=new JFrame("The Oldest");
            String a=JOptionPane.showInputDialog(frame, "Enter, please, the first name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);
           String b=JOptionPane.showInputDialog(frame, "Enter, please, the second name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);


            String age1=a.replaceAll("[^\\d]","");
            String age2=a.replaceAll("[^\\d]","");



            String name1=a.replaceAll("\\d","");
            String name2=b.replaceAll("\\d","");


            int age1int=Integer.parseInt(age1);
            int age2int=Integer.parseInt(age2);



            if (age1int>age2int){
                JOptionPane.showMessageDialog(frame, name1+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
            }

            if (age2int>age1int) {
                JOptionPane.showMessageDialog(frame, name2+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
            }

     }
}

Solution

  • Your both ages are same and your if condition will not match since you are not considering the equality. I think you missed this, Next time try with debugger then you can identify these kind of issues by your own.

     String age1=a.replaceAll("[^\\d]","");
     String age2=a.replaceAll("[^\\d]","");
    

    This should change this to following.

      String age1=a.replaceAll("[^\\d]","");
      String age2=b.replaceAll("[^\\d]","");