Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem with a logic error. The program executes properly, but what should be added in the code to make it work correctly. At the moment, I brought it to the level where it prints ...go for it every time, no matter what numbers I enter.

The user should get the message No No if the conditions are not met?

What I have tried:

Java
import javax.swing.JOptionPane;
public class Main {

	public static void main(String[] args) {
		String bibi;
        bibi = JOptionPane.showInputDialog("Please enter your age:");
       int year = 20;
       if (year >= 18) {
            System.out.println("All OK - go for it!");
        }
        else
        {
            System.out.println("No, No");
        }
	}
}
Posted
Updated 18-Nov-22 11:48am
v2

You accept a value bibi as a string but do not use it.
You then manually set Year with a value
Then test Year is greater than or equal to 18.

You should set Year to the bibi value, not 20. bibi will need to be converted from string to integer before assigning to Year. This is an exercise that I will ask you to do.
 
Share this answer
 
Good advice from good people is worth its weight in gold!!
Thank you Graeme, I used parseInt and solved it.
I hope that this one of my possible solutions will be an inspiration and useful to others.

package bibi;
import javax.swing.JOptionPane;
public class Bibi {
	public static void main(String[] args) {
	String bibi;
    bibi = JOptionPane.showInputDialog("Please enter your age:");
   int year = Integer.parseInt(bibi);
   if (year >= 18) {
        System.out.println("All OK - go for it!");
    }
    else
    {
        System.out.println("No, No");
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900