2013-02-28 124 views
0

我想检查两个字符串是否相同,第一个字符串是从Pastebin RAW获得的,第二个字符串是保存在我的项目资产文件夹中。该文本是完全一样的,但是当我尝试检查它们是否与错误检查两个字符串是否相同android

if(total.toString().equals(result)){ 
    display.setText(
     "The two files are the same \n Log.txt: " + total.toString() + 
     "\n Pastebin: " + result); 
} else if(total.toString()!=result) { 
    display.setText(
     "The two files arent the same \n Log.txt: " + total.toString() + 
     "\n Pastebin: " + result);  

它直接转到我的否则,如果和显示的是,我试图删除文件,并作出新的Pastebins相同。

完整的代码我用的是这个

InputStream is = getAssets().open("Log.txt"); 
    BufferedReader r = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder total = new StringBuilder(); 
    String line; 
    while ((line = r.readLine()) != null) { 
     total.append(line); 
    } 
    // Loads the text from the pastebin into the string result 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpGet httpGet = new HttpGet("Pastebin url"); 
    HttpResponse response = httpClient.execute(httpGet, localContext); 
    String result = ""; 
    BufferedReader reader = 
     new BufferedReader(
     new InputStreamReader(
      response.getEntity().getContent())); 
    String line1 = null; 
    while ((line1 = reader.readLine()) != null){ 
     result += line1 + "\n"; 
    } 
    // Checks if the pastebin and Log.txt contains the same information 
    if(total.toString().equals(result)){ 
     display.setText(
     "The two files are the same \n Log.txt: " + total.toString() + 
     "\n Pastebin: " + result); 
    } else if(total.toString()!=result) { 
     display.setText(
     "The two files arent the same \n Log.txt: " + total.toString() + 
     "\n Pastebin: " + result); 
    } 

那么有谁能够告诉我,因为它说,它是不一样的我做了什么错在这里?

+0

'否则,如果(total.toString()!=结果)'应该只是'else'。 – 2013-02-28 20:31:43

+0

@JonSkeet它不重复,OP使用'equals()',但他在else语句中有一个错误。 – 2013-02-28 20:38:26

+0

@ Eng.Fouad:不一致。注意'if(total.toString()!= result)' – 2013-02-28 20:42:02

回答

3

的问题是在这些线路:

while ((line = r.readLine()) != null) { 
    total.append(line); 
} 

你忘了换行字符\n

while ((line = r.readLine()) != null) { 
    total.append(line + "\n"); 
} 

如您在result做了什么:

while ((line1 = reader.readLine()) != null){ 
    result += line1 + "\n"; 
} 

此外,注意,

else if(total.toString()!=result) 

应该只是

else 
+0

非常感谢,它工作得很好。 – user2048793 2013-02-28 21:09:56

相关问题