0

I am currently trying to handle what happens with results if there is input received and coming across some troubles, I want to be able to set the text to a specific phrase if there is no result, and want to be able to have the input show correctly if there is. I am currently trying this:

String s = scanningResult.getContents();
    if (s == null){ 
        s="Location Is here,Time is here,Cost is here";
    }
    else {
        s=s;
    }
    String [] s2 = s.split(",");

    TextView location = (TextView)findViewById(R.id.LocationResult);
    location.setText(s2[0]);


    TextView time = (TextView)findViewById(R.id.TimeResult);
    if (s2[1] == "Time is here"){ 
        time.setText("Time is here");
    }
    else {
        time.setText(s2[1]+" Seconds");
    }


    TextView cost = (TextView)findViewById(R.id.CostResult);
    if (s2[2] == "Cost is here"){ 
        cost.setText("Cost is here");
    }
    else {
        cost.setText("$"+s2[2]);
    }

}

The problem i am having is that on a null scanningResult the two values time and cost will come out as Time is here Seconds and $Cost is here. And i don't see why.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
Teh john
  • 41
  • 11
  • did you debug the code on a null scanningResult ? To see if the s is really null ? By the way you should do the check by : if (string.IsNullOrEmpty(s)) – NeedAnswers Jun 15 '14 at 08:43
  • Check what is inside `s2` directly after you assigned s2. Maybe you forgot to escape "," or the string has another format as you expected. So giving out `s` is a good idea, too. BTW, your first else is nonsense, what should `s=s;` do? – usr1234567 Jun 15 '14 at 08:58
  • @hoangnnm ,To retrieve the null that i am testing i am simply canceling the scan which sets `scanningResult.getContents()` to null. If i make `String s = null;` it gives me the same result as a canceled scan – Teh john Jun 15 '14 at 09:01
  • @usr1234567 What? And; I just put it in there as a fallback (Aka yes pointless just a habit) – Teh john Jun 15 '14 at 09:02
  • @Tehjohn Than it is a bad habit. It confuses the reader and may introduce bugs for pointless code. Would you mind to change the title to be more meaningful? – usr1234567 Jun 15 '14 at 09:03
  • All you need to do is very simple, like usr1234567 said, just check what is in s2 after you assigned it – NeedAnswers Jun 15 '14 at 09:06
  • Removed it and still no difference, What would you suggest for a title, I chose that cause i couldnt think of one – Teh john Jun 15 '14 at 09:06
  • @hoangnnm, Could i have some example code, I dont quite understand what i am meant to do – Teh john Jun 15 '14 at 09:09
  • MessageBox.Show(s2[1]) ??? – NeedAnswers Jun 15 '14 at 09:16
  • 2
    @hoangnnm, Bills answer fixed the problem, Was just using the wrong method – Teh john Jun 15 '14 at 09:26

1 Answers1

4

in Java use equals method to compare String

if (s2[1].equals("Time is here")){ }
Ioan
  • 5,152
  • 3
  • 31
  • 50
Bills
  • 768
  • 7
  • 19
  • Cant use text, has to be an object for .equal – Teh john Jun 15 '14 at 09:08
  • 1
    @Tehjohn text is String and thus an object, just try it. – A4L Jun 15 '14 at 09:11
  • Additional explanations about @Bill's solution can be found here: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. Is it possible that () are missin arround the literal ? – Christophe Jun 15 '14 at 09:14