2016-10-02 58 views
-3

對不起,稍微改變了這個問題。if(s1.Contains(s2))似乎永遠是真的

本質上我想知道如果aString包含String。我的問題是比較時說aS一個子串aString"aS".contains("String")顯示爲真。

String a="st", b="string"; 我跑System.out.println(a.contains(b)); 即返回false,符合市場預期。我有一個包含的理解,我必須失去其他東西。

所以看來我的程序工作正常,但我做了一些調整,然後回來,整個事情都停止了。我懷疑什麼是通常的常見肇事者(brackets, file io, etc.)。我發現if(string.contains(string))會持續運行,即:始終爲真。不知道爲什麼會發生這種情況,可能是我在代碼中遺漏的東西。

這是我輸出的一個例子(只是一個字符由文件的字符讀):

I n t e g e r G ;

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class comp{ 

    public static void main(String[] args){ 
     ArrayList<String> lines = new ArrayList<String>(); 
     ArrayList<String> symbolTable = new ArrayList<String>(); 
     ArrayList<String> parsedFile = new ArrayList<String>(); 


     try { 
       File file = new File("symbolTable.txt"); 
       Scanner scanner=new Scanner(file); 
       while (scanner.hasNextLine()&&symbolTable.add(scanner.nextLine().replaceAll("\\s+","").toLowerCase())); 
       scanner.close(); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 


     try { 
       File file = new File("APU_CS400_input.txt"); 
       Scanner scanner=new Scanner(file); 
       while (scanner.hasNextLine()&&lines.add(scanner.nextLine().replaceAll("\\s+","").toLowerCase())); 
       scanner.close(); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 


     //runs through line by line of the input file 
     for(String line: lines){ 
      String sBuild = ""; 
      StringBuilder identifier = new StringBuilder(""); 

      //moves through the line char by char 
      for(int i=0;line.length()>i; i++){ 
       sBuild+=line.charAt(i); 
       //moves through the symbol table comparing each symbol to each string 
       //that is built char by char 
       for(String symbol: symbolTable){ 
        //if the char string matches the symbol then any identifiers are saved and 
        //symbols are saved, the string is then reset to empty 
      //This is where i seem to get an issue 

     ***if(sBuild.contains(symbol)){*** 
         if(symbol.length()<sBuild.length()){ 
          identifier.append(sBuild,0,sBuild.length()-symbol.length()); 
          parsedFile.add(identifier.toString()); 
          identifier.delete(0,sBuild.length()-symbol.length()); 
         } 
         sBuild=""; 
         parsedFile.add(symbol); 
        } 

       } 
      } 
     } 


     for(String symbol:parsedFile){ 
      System.out.println(symbol); 
     } 

    } 

} 

Blockquote 
+2

爲什麼會不會是真的? – Zarwan

+1

事實上,它總是**應該是真實的。 –

+1

「對不起,稍微改變了這個問題」 - 這是一個錯字,它應該讀「對不起,將問題從根本上改變爲完全不同的東西」。 – ajb

回答

0

本質上我想知道「aString」是否包含「String」。

是,"aString"爲字符串值確實包含的"String"

的字符串值進行比較時講「爲」(的「ASTRING」子串)「作爲」我的問題是。 contains(「String」)顯示爲true。

確定嗎?這不能是,所以我寧可懷疑你的代碼中的錯誤。


爲了保全 「空字符串符號」 的youself考慮這個問題:

try { 
    File file = new File("symbolTable.txt"); 
    Scanner scanner=new Scanner(file); 
    while (scanner.hasNextLine()) { 
    // toLowerCase will do nothing for characters that are not letters 
    // Don't spend CPU cycles with regex 
    String symbolLine=scanner.nextLine().toLowerCase(); 
    // Collect the symbol only if non-empty?? This will save you from empty symbols 
    if(symbolLine.trim().length()>0) { 
     symbolTable.add(symbolLine); // or .add(symbolLine.trim()) ??? 
    } 
    } 
    scanner.close(); 
} catch (Exception ex) { 
    ex.printStackTrace(); 
} 
+0

當然,生病給我一個嘗試謝謝。 – user3674515

+0

接受了您的建議,並讓我回答了問題,在我的文件末尾沒有考慮換行符。男人是令人沮喪的,並解釋了爲什麼我編輯我的符號表之前工作。謝謝! – user3674515

+0

你曾經提到過,正則表達式是一種清除空白的緩慢方法,所以我使用trim進行了修改。你會推薦什麼去除「/ n」?因爲我目前使用正則表達式.replace(「\ n」,「」)。 – user3674515

1

if(String.Contains(""))總是應該是真實的,只要字符串不是空值。

+0

對,雖然稍微超出了上下文,但我重新提出了我的問題以使其更有意義。 – user3674515

+0

@ user3674515:斷章取義嗎?你的問題標題完全是borked。 –

3

想想這樣。

s1.contains(s2) 

應該返回true,如果s1一個子都可以找到這樣

s1.substring(i, j).equals(s2) 

是真實的。

如果s2是一個空字符串,則i = 0, j = 0就是這樣的一個子字符串,所以返回true

因爲它應該。

+0

會「st」.contain(「string」)爲真? – user3674515

+0

@ user3674515當然不是。你能找到一個等於'「string」''的「st」的子串嗎? No. – Andreas

0

你可能不得不看看這個有點數學,看看爲什麼s.contains("")永遠是真的。假設你認爲這是這樣的:如果有一些值ij這樣a.substring(i,j)等於b

a.contains(b)是真實的。

如果你想想一下,你會發現這是當參數是像"xyz"爲非空字符串正是contains手段。如果有xsubstring等於"xyz",則s.contains("xyz")爲真。如果沒有這樣的子字符串,那麼s.contains("xyz")是錯誤的。

所以它是有道理的,同樣的邏輯將適用於一個空字符串,因爲它適用於其他地方。並且a.substring(0,0)等於""(如果a不爲空)總是如此。這就是爲什麼a.contains("")應該始終如此。

從「包含」的英文含義來看,它可能並不直觀,但是當您處理這樣的「邊緣案例」時,您有時必須以不同的方式思考。通常,Javadoc會將事情說出來,以便您可以輕鬆找出邊緣案例中發生的情況,而無需依賴直覺。不幸的是,在這種情況下,他們沒有。

+1

不,在我發佈之前我沒有看到Andreas的回答,儘管我們碰巧使用了相同的索引變量名稱。我認爲「偉大的思想相似」適用於此... :) :) – ajb

+0

對,空字符串總是匹配。我的問題(修訂)是爲什麼當我有一個字符串a =「我」和字符串b =「整數」,並且我說a.contains(b)它是否導致爲真。 – user3674515

+1

你不應該這樣修改你的問題。修改問題以提供更多信息或更正錯誤或改進措辭或格式是可以的。但是當人們發佈答案後,當你把它變成一個完全不同的問題時,你會讓答題者看起來像白癡。這不是一種善待志願者幫助他人的好方法。如果您有其他問題,請發表一個不同的問題。 – ajb

相關問題