Captain _Schnabo
Captain _Schnabo

Reputation: 11

equalsIgnoreCase is called on itself. Error

My exercise is to create a String variable called "Hallo". I should check, if the String is called "Hallo" with a scanner. If its true, my code should answer with "Hallo". If not, the code should answer with "Tschüss".

Here is the code:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {

        String hallo = "Hallo.";

        Scanner input = new Scanner(System.in);
        {
            System.out.println(input);

                input.nextLine();
                

            if (hallo.equalsIgnoreCase(hallo)) {
                System.out.println(hallo);
            } else {
                System.out.println("Tschüss.");
            }
        }
    }
}

Upvotes: 0

Views: 135

Answers (1)

Hpn4
Hpn4

Reputation: 119

input.nextLine(); returns a String. You need to store the result into a variable like String userResponse = input.nextLine(); then in your condition do if(userResponse.equalsIgnoreCase(hello)). Because in your code you test if your variable hello is equals to your variable hello. It can only be true. You need to store the response of the user and then test if it's equal to your variable hello

Upvotes: 1

Related Questions