1

I want to compare three couples of input from Scanner (just a quick test) by using the Compare method.

When I run this program, it returns false for all three inputs even though there should be a true in between. When I remove Scanner and put in a String myself, I get the correct result

First I thought that it's caused by the extra "Enter" button that stays in the buffer, but when I debugged it, there's nothing unwanted in the buffer and an extra call for input won't do any help.

import java.util.Scanner; 

 public class MainSector {
    String Primary, Secondary;

    MainSector(String Primary, String Secondary){
        this.Primary = Primary;
        this.Secondary = Secondary;
    }

MainSector(int Primary, int Secondary) {
    this.Primary = String.valueOf(Primary);
    this.Secondary = String.valueOf(Secondary);
    }

boolean Compare(MainSector x){
    return (x.Primary == Primary && x.Secondary == Secondary );

    }

}


public class Test {
    public static void main(String[] args) {
        Scanner u = new Scanner(System.in);
        Scanner y = new Scanner(System.in);

        System.out.println("Enter String:");
        MainSector Sector1 = new MainSector(u.nextLine(),y.nextLine());

        System.out.println("Enter String:");
        MainSector Sector2 = new MainSector(u.nextLine(),y.nextLine());

        System.out.println("Enter Integer:");
        MainSector Sector3 = new MainSector(u.nextInt(),y.nextInt());


        System.out.println(Sector1.Compare(Sector2));
        System.out.println(Sector1.Compare(Sector3));
        System.out.println(Sector2.Compare(Sector3));


    }
}

It returns false three times here even if the first two inputs are exactly the same. For example, I get the correct result with this:

System.out.println("Enter String:");
MainSector Sector1 = new MainSector("aa","bb");

System.out.println("Enter String:");
MainSector Sector2 = new MainSector("aa","bb");

System.out.println("Enter Integer:");
MainSector Sector3 = new MainSector(45,65);

It returns:

true

false

false

Spooky
  • 2,966
  • 8
  • 27
  • 41
James
  • 99
  • 1
  • 9

1 Answers1

3

Your problem lies in your compare method; using == compares reference values. Since you are using different object in your comparisons, they will never have the same reference value in this case. You need to use .equals(Object) to compare the exact values. I have edited the code below to reflect this and tested it.
Also, you really don't need 2 scanners to do this, you can use one! As always, please remember to close your scanners; it's good practice.

import java.util.Scanner; 



 class MainSector {
    String Primary, Secondary;

    MainSector(String Primary, String Secondary){
        this.Primary = Primary;
        this.Secondary = Secondary;
    }

MainSector(int Primary, int Secondary) {
    this.Primary = Integer.toString(Primary);
    this.Secondary = Integer.toString(Secondary);
    }

//Note the changes made to this method!
    boolean Compare(MainSector x){
        return ((x.Primary.equals(Primary)) && (x.Secondary.equals(Secondary)));

        }
} //End MainSector


public class Test {
    public static void main(String[] args) {
        Scanner u = new Scanner(System.in);

        System.out.println("Enter String:");
        MainSector Sector1 = new MainSector(u.nextLine(),u.nextLine());

        System.out.println("Enter String:");
        MainSector Sector2 = new MainSector(u.nextLine(),u.nextLine());

        System.out.println("Enter Integer:");
        MainSector Sector3 = new MainSector(u.nextInt(),u.nextInt());


        System.out.println(Sector1.Compare(Sector2));
        System.out.println(Sector1.Compare(Sector3));
        System.out.println(Sector2.Compare(Sector3));

        u.close();
    } //End main
} //End Test


I found a great post, where the answer is explained in extreme detail if you wish to know more: How do I compare Strings in Java?

Community
  • 1
  • 1
Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36
  • thanks man, very thorough! but I really have a hard time understanding what part of my program is addressing a "Reference" instead of a "value" and how come replacing the Scanner with actual Strings have changed "Value" to "Reference" – James Mar 03 '15 at 10:06
  • 1
    When you compare addresses, this is known as "by-reference" when you compare the exact data in question; contents of a variable, this is"by-value". So, when you are doing your compare with == you are comparing the addresses (references) and not the contents (values). – Evan Bechtol Mar 03 '15 at 13:03