0

I'm making a Java program that greets people. Two people, Alice and Bob, are very special, so they get their own, special greetings. Because this will, in the future, get lots of inputs, I wanted to create a convenience function that takes a prompt as its argument, prints the prompt to STDOUT, and then returns the input the user added. (This works like input() in some other languages.) Here is my code:

package test;

import java.util.Scanner;

public class Application {

    static Scanner scanner = new Scanner(System.in);
    public static String userInput(String prompt) {
        System.out.printf(prompt);
        String caught_string = scanner.nextLine();
        return caught_string;
    }
    public static void main(String[] args) {
        System.out.println("Printing test");
        String name = userInput("Can I have your name, please? ");
        if (name == "Bob") {
            System.out.println("You're Bob! Hello, there!");
        } else if (name == "Alice") {
            System.out.println("Alice! Good to see you!");
        } else {
            System.out.println("You must be new here. Hello!");
        }
    }

}

But when I enter Bob, all I get is the else message, You must be new here.. Can someone help me, and tell me where I went wrong?

ASCIIThenANSI
  • 855
  • 9
  • 27

0 Answers0