0

I am a beginner programmer and I was trying to create a random code involving scanner and objects, but I came into a problem involving scanner. Here was my code:

    package jrJava.objectPracticeHome;

import java.util.Scanner;

public class Coordinator {

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    GroceryStore groceryStore = new GroceryStore();
    System.out.println("Welcome to the Zhenda Grocery Store! We sell pears, apples, and oranges!");
    System.out.println("Type in 'apple', 'orange' or 'pear' to view the number of each fruit. ");

    String fruit = scanner.next();

    if(fruit == "apple"){
        System.out.println(groceryStore.getAppleInv());
    }
    else if(fruit == "orange"){
        System.out.println(groceryStore.getOrangeInv());
    }
    else if(fruit == "pear"){
        System.out.println(groceryStore.getPearInv());
    }



}

}

I'm sure I called the methods correctly, and no errors pop up when I run it. Please help! :P Thanks!

  • Use `fruit.equals("apple")` instead of `fruit == "apple"`. –  Mar 06 '16 at 03:20
  • Actually, so you don't get confused in the future, `==` doesn't work on **any** object in Java, only with primitives (long, int, short, char, byte, boolean) and `null`. For any kind of object, you have to define and use a `equals` method to compare two objects. Of course types that already exists will pretty much always have a proper `equals` method. – OliPro007 Mar 06 '16 at 03:25

0 Answers0