2014-12-20 88 views
0

由於某些原因,它突出顯示了布爾和字符串作爲錯誤,我直接從教科書中複製了這段代碼,爲什麼它不工作? 包裝實踐;從Java開始,標記上的語法錯誤

public class practice{ 

public boolean isUniqueChars(string str){ 
if (str.length() > 256) 
      return false; 

     boolean[] char_set = new boolean[256]; 
     for (int i = 0; i< str.length(); i++){ 
      int val = str.charAt(i); 
      if (char_set[val]) { 
       return false; 

      char_set[val] = true; 
     } 
     return true; 
    } 
} 
Errors: Multiple markers at this line 
- string cannot be resolved to a type 
- Syntax error on token "boolean", @ 
expected 
- Syntax error on token ")", -> expected 
- Syntax error on token(s), misplaced 
construct(s) 
+0

你的類定義在哪裏,從git鏈接它只有方法體,你的意思是你複製了方法到你的類體?請發佈您的完整代碼。 –

+0

您正試圖將您的值存儲在char索引中,並且數組索引可以僅爲int。將char_set [val]替換爲char_set [i]。 –

+0

public boolean isUniqueChars(string str) - 字符串應以caps開頭「String」 – aparna

回答

1

那麼試試這個:

package practice; 

/** 
* 
* @author manoj.sharma 
*/ 

public class Test{ 
public static void main(String [] a){ 
System.out.println(new Test().isUniqueChars("Hello world")); 
} 
public boolean isUniqueChars(String str){ 
    if (str.length() > 256) 
     return false; 

    boolean[] char_set = new boolean[256]; 
    for (int i = 0; i< str.length(); i++){ 
     int val = str.charAt(i); 
     if (char_set[val]) { 
      return false; 
     } 
     char_set[val] = true; 
    } 
    return true; 
} 
} 
+0

那麼現在沒有錯誤,但運行它說theres沒有以前啓動,爲什麼我的代碼不會執行? – FullCombatBeard

+0

我發現的問題是錯字錯誤。你已經寫了String作爲字符串,String的第一個字母必須是大寫的,因爲String是一個不是原始的類。 –

+0

當我點擊綠色運行錯誤,它說: 無法啓動:選擇無法啓動,並且最近沒有啓動 – FullCombatBeard

0
public boolean isUniqueChars(string str){ 

似乎是一個錯字,應該String

0

使用 「S」 在Java中的String聲明。

isUniqueChars(String str)