2016-07-23 44 views
0

我得到使用JDK 1.3 String[] t = words.split("_");一個錯誤的IntelliJJava的分裂在JDK 1.3

Error:(133, 51) java: cannot find symbol 
    symbol: method split(java.lang.String) 
    location: variable words of type java.lang.String 

我有因爲項目纔可以使用此SDK,我嘗試JDK 1.4,但有許多其他錯誤,然後我決定用一些可以使用jdk 1.3編寫的代碼替換上面的代碼。

這是什麼功能?

+0

很抱歉,但不只是用java的舊版本,而嘗試更新它(取決於其大小) – ScriptKiddy

回答

0

您將不得不使用StringTokenizer,indexOf()和substring()的組合,或者您自己創建的東西。

0

你可以使用C方法,即:自己實現它。

這裏是一個可能的實現,現在返回的所有元素,可能需要一些調整:

int length; 
    int split_amount = 0; 
    String temp = new String("This_takes_into_consideration_something_something_test"); 
    char split = '_'; 
    for(int i = 0; i<length;i++){ 
     if(temp.charAt(i) == split){ 
      split_amount++; 
     } 
    } 
    split_amount++; 
    String[] result = new String[split_amount]; 
    int j = 0; 
    for(int i = 0; i<split_amount; i++){ 
     result[i] = ""; 
     boolean t = true; 

     for(; j<length && t ;j++){ 
      if(temp.charAt(j) == split){ 
       t = false; 
       break; 
      } 
      result[i] += temp.charAt(j); 
     } 
     j++; 
    } 
+0

認爲他應該更容易使用StringTokenizer,儘管我不是100%確定的,因爲我從來不需要使用舊的JDK並使用那個而不是split()方法。 – Memfisto

+0

@Memfisto如果他不想自己實現它,對他來說可能會更容易。我剛剛完成了他的問題的實施並編輯了我的評論。 –

1

public String[] split(String regex)introduced in Java 1.4

所以,你可以使用使用StringTokenizer(String str, String delim)自己實現這是introduced in Java 1.0

List list = new ArrayList(); 
StringTokenizer st = new StringTokenizer("this_is_a_test", "_"); 

while (st.hasMoreTokens()) { 
     list.add(st.nextToken()); 
} 
//[this, is, a, test] 

此外,如果你想最終結果作爲一個數組,你可以使用

String[] t = list.toArray(new String[0]); 
+0

謝謝,但它說泛型不支持 – Ahmad

1

下面這段代碼似乎對我來說工作得很好。

但是,我認爲您需要分割的分隔符只是一個字符。

public static void main(String[] args){ 
    String string = ",alpha,beta,gamma,,delta"; 
    String[] wordsSplit = splitByDelimiter(string, ","); 
    for(int i=0; i<wordsSplit.length; i++){ 
     System.out.println("-"+wordsSplit[i]+"-"); 
    } 
} 

public static String[] splitByDelimiter(String fullString, String delimiter){ 
    // Calculate number of words 
    int index = 0; 
    int[] delimiterIndices = new int[fullString.length()]; 
    int wordCount = 0; 
    do{ 
     if(delimiter.equals(fullString.charAt(index)+"")){ 
      delimiterIndices[wordCount++] = index; 
     } 
     index++; 
    } while(index < fullString.length()); 

    // Correction for strings not ending in a delimiter 
    if(!fullString.endsWith(delimiter)){ 
     delimiterIndices[wordCount++] = fullString.length(); 
    } 

    // Now create the words array 
    String words[] = new String[wordCount]; 
    int startIndex = 0; 
    int endIndex = 0; 

    for(int i=0; i<wordCount; i++){ 
     endIndex = delimiterIndices[i]; 
     words[i] = fullString.substring(startIndex, endIndex); 
     startIndex = endIndex+1;    
    }  
    return words; 
} 

替代的解決方案:

public static ArrayList splitByDelimiter(String fullString, String delimiter){ 
    fullString += delimiter; // 
    ArrayList words = new ArrayList(); 
    int startIndex = 0; 
    int endIndex = fullString.indexOf(delimiter); //returns first occurence 
    do{ 
     words.add(fullString.substring(startIndex, endIndex)); 
     startIndex = endIndex+1; 
     endIndex = fullString.indexOf(delimiter, startIndex); 
    } while(endIndex != -1); 

    return words; 
} 
0

也許一個簡單的解決方案是:

String words = "this_is_a_test"; 
    StringTokenizer st0 = new StringTokenizer(words, "_"); 
    String[] t = new String[st0.countTokens()]; 
    int k = 0; 
    while(st0.hasMoreTokens()){ 
     String tmp0 = st0.nextToken(); 
     t[k] = tmp0; 
     k++; 
    }