2017-03-13 48 views
-3

我一直试图检查两行是否具有相同的内容,以便在完成所有操作时将其保留在文件之外。但是,检查两者是否相似的if语句无法正常工作。即使这些界限看起来很相似,if语句仍然认为它是错误的。为什么从一个文件中的这一行与具有相同字符的字符串不一样?

String fileLine = ""; 
String comp = ""; 
BufferedReader tempRead = new BufferedReader(new FileReader(new File("res/save.sav"))); 
PrintWriter tempWrite = new PrintWriter(new FileWriter(new File("res/tempSave.sav"))); 
for(String s: addToSave){ 
    while((fileLine = tempRead.readLine()) != null){ 
     comp = s.substring(0, s.length()-4); 
     System.out.print(fileLine.substring(0, comp.length()-1)); 
     System.out.print(comp); 
     if(fileLine.substring(0, comp.length()-2).equals(comp)){ 
      System.out.println("is gud"); 
     } 
    } 
} 
tempWrite.close(); 
tempRead.close(); 
+0

好吧,请记住,当你谈论电脑时,“看似相似”绝对是“不一样”。 – Craig

+0

在另一个语句中,你有'fileLine.substring(0,comp.length() - 1)',在另一个语句中有'fileLine.substring(0,comp.length() - 2)'。这应该是什么? –

回答

0

你将永远不会有一个长度,comp.length() - 2的字符串,等于一个不同的长度,comp.length()的另一个字符串。

相关问题