2014-09-25 78 views
-3

我對Java有點新意,並且試圖在正確的字符串中寫入這個簡單的程序,使其輸出「Password entered」並在輸入其他內容時會說「密碼錯誤」。但它給了我這個錯誤,我對如何解決沒有任何線索,它說:的語法錯誤標記「其他」,{預期」令牌'else'上的語法錯誤,{預計

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args){ 

     String code = ("1908"); 
     Scanner s = new Scanner(System.in); 
     String input; 

      System.out.println("Please enter password!"); 
     input = s.nextLine(); 

     if(input==code) 
      System.out.println("Password entered!"); 
    } else { //<--- Syntax error on token 'else',{ expected 
      System.out.println("Wrong password!"); 
      } 

     } 
    } 
+1

您的縮進應該是一個很大的線索。 – 2014-09-25 20:22:34

回答

0

在此行中if(input==code)你已經忘記添加打開支架,只需添加{到底。

所以看起來像

if(input==code) { 
    System.out.println("Password entered!"); 
} else { 
    System.out.println("Wrong password!"); 
} 

編輯評論

System.out.println("Password entered!");從不打印的原因是因爲您正在使用==運算符。當使用==運算符比較對象時,它將檢查對象是否引用內存中的相同位置。基本上它會檢查對象名稱是否被引用到相同的內存位置。當你不應該使用==一個例子是像

// Create two Strings with the same text "abc" 
String obj1 = new String("abc"); 
String obj2 = new String("abc"); 

// Compare the two Strings 
if (obj1 == obj2) { 
    // Will never reach here and print TRUE 
    System.out.println("TRUE"); 
} else { 
    // Will always print FALSE 
    System.out.println("FALSE"); 
} 

上面的代碼將始終打印FALSE因爲兩個String對象不位於同一地點的記憶 - 混淆我知道了!爲了澄清時使用的==見下文

// Create a new string object 
String obj1 = new String("abc"); 
// Create another new string object but assign obj1 to it. 
String obj2 = obj1; // obj1 and obj2 are now located in the same place in memory. 

// Compare the two objects 
if (obj1 == obj2) { 
    // Will always print TRUE 
    System.out.println("TRUE"); 
} else { 
    // Will never print FALSE 
    System.out.println("FALSE"); 
} 

代碼在他上面的代碼中我們的兩個對象都位於內存中,因此它們在某種意義上等於同一個地方,當涉及到的內存引用的比較每個對象。

但顯然你不想比較你想比較每個對象包含的字符串值的對象的內存引用。所以你需要看的是equals()方法,我不會詳細討論這個問題,但它基本上比較了兩個對象值而不是內存位置。看看下面的代碼

// Create two Strings with the same text "abc" 
String obj1 = new String("abc"); 
String obj2 = new String("abc"); 

// Compare the two String values using equals() 
if (obj1.equals(obj2)) { 
    // Will always print TRUE because the two string values 
    // are equal/the same. 
    System.out.println("TRUE"); 
} else { 
    // Will never print FALSE 
    System.out.println("FALSE"); 
} 

因此,在使用我們剛纔瞭解了==運營商和equals()方法,我們可以在你的代碼中使用,以便嘗試更改爲

// Using equals because you want to compare the String values 
// not the reference to memory location for each object 
if(input.equals(code)) { 
    // Should print true if input value is equal to code value. 
    System.out.println("Password entered!"); 
} else { 
    System.out.println("Wrong password!"); 
} 

如果你有再有問題只是發表評論,希望這有助於。

+0

謝謝!雖然我在添加{後遇到問題,但它現在不打印「輸入密碼!」了。另外,我改變了} else {to} else if(!code.equals(input)){確保它不檢查特定的字符串。 – 2014-09-25 20:33:23

+0

@FrederikLarsenBang我已經編輯了我的答案,包括一個解釋,爲什麼不在你的場景中使用'=='操作符,以及做什麼。請參閱標記爲**編輯部分的評論** – Popeye 2014-09-26 07:42:24

0

您沒有使用{你如果(打開「真」條款),遇到} else所以當該}實際終止整個if()

它應該是以下幾點:

if (...) { 
    ... 
} else { 
    ... 
} 

if (...) 
    ... 
else { 
    ... 
} 
相關問題