2011-08-08 13 views
0

私は明らかにアンドロイドでは非常に難しいreallllly単純な何かをしようとしています。 2つの文字列を比較して、等しいかどうかを確認したいだけです。文字列の比較の問題

私は「私はこれをデバッグしているし、それが実際に は、だから私は最初

if(temp == "Location") { //do something } 

でこれを試してみました。しかし、私はすでにdoesnのことを知っている...場所が含まれていない値「場所」 で一時変数を持っています仕事。

.equals .contains .ignoreCaseEquals 等...誰か助けてください何をすべきかどんな考えを持っている場合

:私はその後のような文字列のすべての可能な機能を試してみました。これは本当に迷惑になっています。

編集: ここでは、私が見たいと思っているものの文字列を比較している関数です。

public String[] getData(){ 
    try { 
     int tempGroupCount = 0; 
     URL food_url = new URL (Constants.SERVER_DINING); 
     BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream())); 
     temp = my_buffer.readLine(); 
     // prime read 
     while (temp != null){ 
      // check to see if readline equals Location 
      Log.w("HERasdfsafdsafdsafE", temp); 
      // start a new location 
      if (temp.equals("Location") 
      { 
       groups[tempGroupCount] = temp; 
       tempGroupCount++; 
      } 
       Log.w("HERasdfsafdsafdsafE", temp); 
       //start for-loop to test to get child info 
       //for(temp = my_buffer.readLine(); temp != "Location" && temp != null; groupCount++, childrenCount++){ 
        //children[groupCount][childrenCount] = temp; 
       //} 

      temp = my_buffer.readLine(); 
     } 
     my_buffer.close(); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString()); 
    } 
    return groups; 
    } 
+0

場合 'equals'は動作しませんか? – EboMike

+0

これは(たぶん)役立つでしょう:http://stackoverflow.com/questions/4210713/equal-and-equalsignorecase-return-false-for-equal-strings – MByD

答えて

0

あなたの変数tempがStringある場合

if(temp.equals("Location")) { //do something } 

while (!temp.equals("")){ 
0

のようにしてみてください、あなたはこの方法compareTo(String)をも使用することができます。

if (temp.compareTo("Location") == 0) 
{ 
    //do something 
} 
3

equals作業を行います。 temp.equals("Location")がfalseを返す場合は、temp変数ではありません。値が "Location"の文字列を参照してください。

文字列に印刷できない文字やその他の奇妙な文字が含まれている可能性があります。の長さを確認して確認してください。代わりに、がASCII文字のようにと見える他の文字が存在する可能性があります。デバッガで、配列を調べて基礎となる文字配列を取得してみましょう。各文字のUnicode値を確認してください。

+0

私が引っ張っているデータはサーバー上にありますhttp://mtweb.mtsu.edu/mobiledev2/Dining/Food_Times.txtと私はサーバーatm上のファイルを編集するためのアクセス権がありませんので、空白のようにテキストファイルに問題があるかどうか調べることはできませんデバッガで、文字列として各文字列を見ることができます...どのように? – cj1098

+1

Androidには素晴らしいデバッガがあります。それを使用したくない場合は、すべての文字を(length()でサイズを取得)、 'Log.d()'でデバッグ出力に 'charAt'を介してUnicode値を出力するだけです。 。 – EboMike

+0

@ cj1098:あなたはコードにデバッグしたと言います。おそらくあなたはデバッガの使い方を知っています。文字列を調べるときに、文字列の基本的な配列へのアクセスを含む、より「生の」ビューを得ることができるはずです。 –

0

はこれをやってみてください。

if (temp.toLowerCase().compareTo("location") == 0) 
+0

はそれを試しました。 no wuck – cj1098

+0

更新を確認し、何か変更があるかどうか確認してください。 –

0

私は、その作業罰金を同じシナリオをしています。

String result = responsePrimitiveData.toString(); 
if(!result.equals("0")){ 
    } 
2
if(temp.equals("Location")) 
{ 
     //your code here 
} 
does not work 

try this 
if(temp.contains("Location")) 
{ 
     //your code here 
} 
+0

tempはコレクションではなく、その文字列変数です。 –

1
public String[] getData(){ 
    try { 
     int tempGroupCount = 0; 
     URL food_url = new URL (Constants.SERVER_DINING); 
     BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream())); 
     temp = my_buffer.readLine(); 
     // prime read 
     while (temp != null){ 
      // check to see if readline equals Location 
      Log.w("HERasdfsafdsafdsafE", temp); 
      // start a new location 
      if (temp.toString().equalsIgnoreCase("Location") 
      { 
       groups[tempGroupCount] = temp; 
       tempGroupCount++; 
      } 
       Log.w("HERasdfsafdsafdsafE", temp); 
       //start for-loop to test to get child info 
       //for(temp = my_buffer.readLine(); temp != "Location" && temp != null; groupCount++, childrenCount++){ 
        //children[groupCount][childrenCount] = temp; 
       //} 

      temp = my_buffer.readLine(); 
     } 
     my_buffer.close(); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString()); 
    } 
    return groups; 
    } 

、それを比較する文字列に「TEMP」を変換し、この5月にはあなたがあなたの問題がどこにあるかを見つけるために、以下を試して

0

を助け適用する最初の試み。

これは、両方のストリングのバイト表現を標準出力に出力するはずです。 equals()がfalseを返す場合、文字列は異なります。印刷できない文字や類似した文字のため、その違いを見つけることが困難な場合があります。しかし、バイト表現はあなたに表示されるはずです。

(私はアンドロイドのプログラマーではないので、私は機能がアンドロイドJVM上に存在する希望とタイプミスや不足しているブラケットのため申し訳ありません - 。何についての;-)