0

I'm new to this site :)

Anyway, I decided to learn java a couple of days ago and I have a small problem which I don't know how to fix. This is my code: When name = Jim I want it to output "Hello Jim", when it doesnt = Jim then output "Youre not Jim, youre " + name

import java.util.*;
public class app {
    public static void main(String[] args) {
        String name;
        Scanner inputName = new Scanner(System.in);

        System.out.println("Enter a name: ");
        name = inputName.nextLine();

        if (name == "Jim") {
            System.out.println("Hello " + name);
        } else {
            System.out.println("Youre not Jim! You are " + name);
        }

    }
}
insideman11
  • 33
  • 1
  • 4

2 Answers2

1
    if (name == "Jim")

This is wrong. You compare Strings in Java with .equals().

    if (name.equals("Jim"))

You should always compare object references meaningfully. Understand that the == operator compares object references and primitive values only. You don't want to compare object references in this case, you want to compare the objects meaningfully. The .equals() implementation of the String class does that for you.

Kon
  • 10,702
  • 6
  • 41
  • 58
0

change to

 if (name.equalsIgnoreCase("Jim")) {
Johan
  • 475
  • 4
  • 17