- Home /
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!
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.
I would also suggest to explicitly declare your types for variables.
Hi $$anonymous$$arowi and efge, thanks for the quick response. Indeed I think you guys had hit the jackpot. When I change the ifcheck to '!=' ins$$anonymous$$d 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 = "<$$anonymous$$alay>";" 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!
@$$anonymous$$arowi, efge: neither C# nor JS require explicitly declaring types. Type inference takes care of it. Writing out the type changes exactly nothing.
Answer by David 3 · Feb 25, 2011 at 10:07 AM
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
Hi $$anonymous$$. 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!
Your answer
Follow this Question
Related Questions
Question on Possible FOR Loop Condition Conflict 0 Answers
if statement not returning true 1 Answer
Create DoTween from for loop? 1 Answer
Using i in if statement 3 Answers