Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么使用java scanner从txt文件读取的字符串不等于相同的字符串?_Java_Arrays_If Statement_Text Files_Java.util.scanner - Fatal编程技术网

为什么使用java scanner从txt文件读取的字符串不等于相同的字符串?

为什么使用java scanner从txt文件读取的字符串不等于相同的字符串?,java,arrays,if-statement,text-files,java.util.scanner,Java,Arrays,If Statement,Text Files,Java.util.scanner,下面是我的代码,目标是从txt文件中读取指令,使java robot执行这些指令。将if语句的任何部分更改为!=机器人部分工作,因此我不得不认为字符串在某种程度上是不相等的 public void interpretGoo() throws AWTException, FileNotFoundException, InterruptedException{ Scanner scanner = new Scanner( new File("instruct.txt") ); St

下面是我的代码,目标是从txt文件中读取指令,使java robot执行这些指令。将if语句的任何部分更改为!=机器人部分工作,因此我不得不认为字符串在某种程度上是不相等的

public void interpretGoo() throws AWTException, FileNotFoundException, 
InterruptedException{
    Scanner scanner = new Scanner( new File("instruct.txt") );
    String in;
    String instruct = "";
    while(scanner.hasNextLine()== true){
        in = scanner.nextLine();
        instruct+= in;
    }
    scanner.close();

    String[] instructSet= instruct.split("-");
    System.out.println("String: " + instruct);

    String[] command= new String[10];
    for(int i= 0; i< instructSet.length;i++){
        command= instructSet[i].split(" ");
        System.out.println("Set: " + instructSet[i]);
        System.out.println("Word: " + command[0]);
        if(command[0].trim()== "key"){
            keyboardHandler(command[1].charAt(1));
            Thread.sleep(150);
        }else if(command[0].trim()== "clickL"){
            accuMouse(Integer.parseInt(command[1]),Integer.parseInt(command[2]), 1, false);
        }else if(command[0].trim()== "clickR"){
            accuMouse(Integer.parseInt(command[1]),Integer.parseInt(command[2]), 0, true);
        }else{
            System.out.println("FAIL");
            continue;
        }
    }
}

根据输出,字符串是相同的,但未通过检查,任何帮助将不胜感激

在Java中,需要使用
.equals
进行字符串比较。如果只使用
=
,您会发现许多相同的字符串彼此不相等。大多数对象也是如此。一般来说,在Java中使用
.equals()
来实现相等更安全

只有字符串的内容相同。但不是对象本身<代码>=比较的是身份,而不是内容。使用
equals
比较内容。还请注意,与
true
false
的比较已经过时:
while(foo==true)
while(foo)
相同。这对我来说似乎是违反直觉的,但确实有效。最难的问题是你不知道你不知道的问题。非常感谢。请查看以下说明:
String: clickL 728 1062-clickL 540 382-key h-key e-key l-key l-key o-
Set: clickL 728 1062
Word: clickL
FAIL
Set: clickL 540 382
Word: clickL
FAIL
Set: key h
Word: key
FAIL
Set: key e
Word: key
FAIL
Set: key l
Word: key
FAIL
Set: key l
Word: key
FAIL
Set: key o
Word: key
FAIL