x


If statement within the For-Loop is not working - Puzzled!

Hi all again,

I am having some puzzling issue with a If-statement within a For-loop. In brief, the script is supposed to, in the beginning, read a txt file into an array, and subsequently the If-statement-within-For-loop will identify the exact text needed and returns the value.

The txt file, named Language.txt, goes like this:

<English>
<Chinese>
<Malay>
<Tamil>

The script that I crafted goes like this:

function Start () {
    var fileName = "Language.txt";
    var sr = new StreamReader(Application.dataPath + "/" + fileName);
    var fileContents = sr.ReadToEnd();
    sr.Close();
    var Languages = fileContents.Split("\n"[0]);

    var textNeeded = "<Malay>";
    for (var j : int = 0; j < Languages.Length; j++) {
        if (Languages[j] == textNeeded) {
            print(textNeeded);
        }
    }
}

Up till the part for the loop, the script has no problem, as if I insert a print(j) before the if-statement, all the j values (from 0 to 3) were returned in the debug console. And if I do print(Languages[j]) before (or after) the If-statement, all the Languages[j] are also returned. The one that is not working is the if-statement. Somehow it is not being activated (for lack of a better word).

I have look thru a number of questions/answers here in UnityAnswer, and it seems the script above is not too different from some of these questions/answers (e.g. like this, and theirs are working.). Am I missing something here?

Thanks in advance!

more ▼

asked Feb 25 '11 at 08:05 AM

Nik gravatar image

Nik
6 3 3 8

I'm not totally familiar with the StreamReader Class - is .Split() going to return an array of strings? You may also wish to explicitly declare your types. I suspect if you change your ifcheck to "if (Languages[j] != textNeeded)", it will print(textNeeded) four times, and it's actually that the variables aren't equivalent, rather than the ifcheck failing.

Feb 25 '11 at 08:14 AM Marowi

I would also suggest to explicitly declare your types for variables.

Feb 25 '11 at 08:27 AM efge

Hi Marowi and efge, thanks for the quick response. Indeed I think you guys had hit the jackpot. When I change the ifcheck to '!=' instead of '==', I do get four print(textNeeded). I took your advices and tried declaring the 2 variables, for quote: "var Languages : String[] = fileContents.Split("n"[0]);" unquoted, and quote "var textNeeded : String = "";" unquoted, to explicitly make both variables as String. But it is still working if the ifcheck is "==". Is String[] not the same as String? Thanks!

Feb 25 '11 at 08:43 AM Nik

@Marowi, efge: neither C# nor JS require explicitly declaring types. Type inference takes care of it. Writing out the type changes exactly nothing.

Feb 25 '11 at 08:59 AM Eric5h5

@Nik: print out each line, so you can see what you're actually getting.

Feb 25 '11 at 09:02 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

my guess is that your split function is leaving some white space characters before or after each element of Languages.

try:

var textNeeded = "<Malay>";    
 for (var j : int = 0; j < Languages.Length; j++) { 
      if (Languages[j].Trim() == textNeeded) {            
      print(textNeeded);       
   }  else {
  print("notmatched:'"+Languages[j].Trim()+"'");
  } 

}

if it doesnt work let us know what it prints

more ▼

answered Feb 25 '11 at 10:07 AM

David 3 gravatar image

David 3
624 13 19 29

Hi David. You are spotted on. From the print(), the outputs were not obvious that there were "space(s)" somewhere before or after the elements (I tried "highlighting" over the print() but didn't see anything like spaces). The use of .Trim() solved this, and this script is now working. Adding on, both Languages[j] and textNeeded are implicitly declared as string, hence this is not a variable mis-match issue. Apprieciated your insight and many thanks!

Feb 25 '11 at 11:09 AM Nik

no worries, thanks for the accept :)

Feb 25 '11 at 11:38 AM David 3
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x95
x75

asked: Feb 25 '11 at 08:05 AM

Seen: 914 times

Last Updated: Feb 25 '11 at 08:10 AM