Skip to content
Advertisement

How do I output a message using JFrame depending on whether data is incorrect or correct?

I have used a boolean method to decide whether the data is correct or not. For some reason, even when the data is correct or not the data still shows up as incorrect. The boolean is underlined and states that the variable error is never used. When the report button is clicked it should check if the data is correct or not. If the data is not correct it should show

Data problems

data errors here.

If the data is correct it should show

Medical Report

data here.

here is my code:

if (e.getSource()==reportButton )
    {
        int riskCounter =0;
        boolean error = false;

        String convert = ageField.getText();

        if (error = true){
            log.setText("Data problems n============n");

        }
        else
        {
           log.setText("Medical Report n============n"); 
        }

       try {
          int age = Integer.parseInt(convert);
           if (age < 0 || age > 116)
           {
               log.append("Age can only be between 0 and 116!n");
               error = true;
           }
           else if (age > 70)
           {

               riskCounter++;
               error = false;
           }
           else 
           {
           error = false;  
           log.append("Age:  " + age + "n");    
           }
       }
           catch (NumberFormatException r)
                   {

                       log.append("Integers only for Age!n");
                       error = true;
                   }
     String smoke = smokesField.getText();
     if (smoke.equalsIgnoreCase("y")) 
     {
         error = false;
         log.append("Smokes:   "+smoke +"n");

        riskCounter++;
     }
     else if (smoke.equalsIgnoreCase("n"))
     {  
         log.append("Smokes:   "+smoke +"n");
         error = false; 
     }
     else
     {

         error = true;
         log.append("Smokes must be one of 'y', 'Y','n','N'n");

     }


     String overweight = overweightField.getText();
     if (overweight.equalsIgnoreCase("y")) 
     {
         log.append(overweight +"n");
          error = false;
        riskCounter++;
     }
     else if (overweight.equalsIgnoreCase("n"))
     {
         error = false;
         log.append("Overweight:  "+ overweight +"n");
     }
     else
     {
         log.append("Overweight must be one of 'y', 'Y','n','N'n");
     }

     if (riskCounter <=0)
     {
         log.append("Low Risk");
     }
     else if (riskCounter >0 && riskCounter <2) 
     {
         log.append("Medium Risk");
     }
     else if (riskCounter >=2 && riskCounter <3)
     {
         log.append("High Risk");
     }
     else if (riskCounter >=3)
     {
         log.append("Very High Risk"); 
     }



    }

Advertisement

Answer

I needed to compare the boolean. To check if the error is true:

if (error){
            log.setText("Data problems n============n");

        }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement