2017-10-13 8 views
0

配列の位置0の文字列の値を検出しようとしていますが、文字通りコピーしても値を貼り付けても、それをチェックしても類似性は検出されません。文字列配列を使用した選択

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { 
    // boolean method must return true or false 
    Player player = (Player) sender;  // casts the sender, from CommandSender, to class Player 
    String ref = sender.getName();   // string 'ref' refer's to the sender's name 
    boolean success = f;     // Stores if code is executed correctly 

    if(label.equalsIgnoreCase("blocks")) { // if command sent is '/blocks' 
     success = t;  // This code will work fine if args has no values 
     if (args.length == 0) { 

      some_code() 

     }else{ 
      getLogger().info(args[0].toString());  // for debugging/prints out the 
      String arg0 = args[0].toString(); 
      if(arg0 == "broken") { // This is the bit that wont work. when args[0] is 'broken' the if statement passes to the else claus 

            some_more_code() 

      }else if(arg0 == "placed") { 

       even_more_code()  
     }else { 
       getLogger().info("I failed to run"); // for debugging / it constantly prints this error message 
       success = f;} 

     } 

    } 

私は、Bukkitライブラリを使用しています。 onCommandメソッドが呼び出されると、args []配列が渡されます。私は文字通り、このメソッドにargs []を渡す入力に自分のコードから "broken"と "placed"をコピー貼り付けましたが、それでもfalseを返します。

すべてのヘルプは非常にJavaで

+6

[Javaで文字列を比較するにはどうすればいいですか?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –

答えて

2

文字列をいただければ幸いオブジェクトであり、==を使用してそれらを比較するアドレス比較ではなく、あなたが期待するものを提供します。

代わりに.equals()を使用してください。

+0

ダウンホートは喜んで受け入れられています**建設的なコメントが続く場合**。 – Neo

+0

if(args.length == 0)はなぜ私が尋ねるのを気にしないのですか? –

+1

'args.length'はプリミティブ' int'を返し、 '0'もプリミティブ' int'です...文字列を比較したい場合は、 'equalsIgnoreCase'を使用してください... – assembler

0

==は、「与えられた2つのパラメータは同じ参照です」演算子で、使用する文字列はクラスStringの.Equals()関数です。 提案:ハードコードされた文字列の不一致を避けるために、可能な文字列引数を扱うクラスを使用してください。

関連する問題