0

I am making a Game of Life program, early stages of it. When I run the program and get to the "Do you want to make..." and i input "y", it will go to the else, print my test statement of test3, and end the program. What am I overlooking?

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);

    String userInput = "";
    char[][] initialGrid = new char[25][75];
    char[][] world = makeInitialGrid(kb, userInput, initialGrid);
    printGrid(world);
    userInput = "y";
    while (userInput == "y"){
        System.out.println("Do you want to make a new generation? (y) yes (n) no");
        userInput = kb.nextLine();
        System.out.println(userInput);
        if (userInput == "y"){
            System.out.println("test1");
            int numOfNeighbors = findNeighbors(world, 6, 2);
            System.out.println("test2");
            System.out.println(numOfNeighbors);
            //makeNewGeneration(world);
        } else {
            System.out.println("test3");
            break;

        }
    }
    kb.close();
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

1 Answers1

2

For string comparisons in Java, you need to use String#equals, not ==. Try if (userInput.equals("y")) { ... instead.

Gian
  • 13,735
  • 44
  • 51