java scanner&same-exact-inputs不相等(对我来说?)

时间:2015-03-02 13:45:28

标签: java java.util.scanner

我想比较来自Scanner的三对输入(只是快速测试) 使用Compare方法。

当我运行此程序时,它会为所有三个输入返回false,即使它们之间应该有true。当我删除Scanner并自己输入String时,我会得到正确的结果

首先我认为它是由额外的" Enter"引起的。保留在缓冲区中的按钮,但是当我调试它时,缓冲区中没有任何不需要的东西 并且额外的输入要求不会得到任何帮助。

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));


    }
}

即使前两个输入完全相同,它也会在此返回false三次。例如,我得到了正确的结果:

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);

它返回:

true

false

false

1 个答案:

答案 0 :(得分:2)

你的问题在于你的比较方法;使用==比较参考值。由于您在比较中使用了不同的对象,因此在这种情况下它们永远不会具有相同的参考值。您需要使用.equals(Object)来比较确切的值。我编辑了下面的代码来反映这一点并进行了测试。
此外,你真的不需要2个扫描仪来做这个,你可以使用一个!一如既往,请记得关闭扫描仪;这是一种很好的做法。

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


我找到了一篇很棒的文章,如果你想了解更多,我会在这里详细解释答案:How do I compare Strings in Java?