2

I have a java program below:

package assignment;

import java.util.Scanner;


public class Assignment {

    
    public static void main(String[] args) {
        System.out.println("Enter your age and I wil let you know if you are eligible to vote");
        Scanner input = new Scanner(System.in);
        int age;
        age = input.nextInt();
        String nationality;
        nationality = "ghana";
        nationality = "Ghana";
       
        if (age>=18) {
           System.out.println("Enter your nationality");
           String nation = input.next();
        if (nation.equals(nationality)){
            System.out.println("You are eligible to vote");
        }
        else{
            System.out.println("You are not eligible to vote");
        }
    }
 }

Now I would like to assign the two String values, "Ghana" or "ghana" to the "nationality" variable so that when the user enters either "Ghana" or "ghana" the condition becomes true when the program gets to check the "nation" value to the "nationality" value and then executes the statement in the "if" block.

But with my above code, the program executes the "if" statement only when the user enters "Ghana". The program executes the "else" statement when the user enters "ghana".

Please kindly help me out.

Thank you.

2
  • 2
    you probably just want to compare while ignoring the case. Look into equalsIgnoreCase Commented Mar 4, 2021 at 18:15
  • An alternative solution is to always work in a singular case, e.g. #toLowerCase or #toUpperCase on the user's input. Commented Mar 4, 2021 at 18:17

3 Answers 3

5

Use equalsIgnoreCase()

if (nation.equalsIgnoreCase(nationality))
Sign up to request clarification or add additional context in comments.

5 Comments

Link against the official documentation, not tutorialspoint
And preferably the documentation for a version of Java that is not obsolete.
@k314159 - Google results are a mystery to me. xD Fixed to v15
Thank you @Mike B.. Using the equalsIgnoreCase() worked but I had an exclamation mark at the "if" line.. Find attached an image here: screenpresso.com/=ernJg
@Nana_Yaw - i don't see any issue there. Those warnings can also be ignored. But what does it say if you hover over it?
0

You can do this in several ways

solution 1:

if (nation.equalsIgnoreCase(nationality))

solution 2:

if (nation.toLowerCase("ghana")) // you can use enum instead of hard coded value

solution 3:

if (nation.toUpperCase("GHANA"))

Comments

0

That is happening because you assing the value ghana and the Ghana. This means that when the programm reaches the if statement the value of your variable is "Ghana". I would use the .toLowerCase method

if (nation.equals(nationality) || nation.equals(nationality.toLowerCase())
{
            System.out.println("You are eligible to vote");
}

You can check the documentation here https://www.tutorialspoint.com/java/java_string_tolowercase.htm

1 Comment

Thank you all for the support given.... I've been able to run the code successfully

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.