FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   newbie loadStrings() question
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: newbie loadStrings() question  (Read 1166 times)
Robin


newbie loadStrings() question
« on: Sep 9th, 2004, 2:14am »

Hi there. I'm trying to read a String Array of values created using loadStrings(). All i want to do is check for a particular word in the text file. (Hello);
 
Data.txt simply contains one line, 'Hello' (no quotes)
 
Code:
void setup(){
  String myArray[] = loadStrings("data.txt");
  for (int i=0; i<myArray.length; i++) {
    println(myArray[i]);  // returns "Hello"
  }
 
  if (myArray[0] == "Hello") {  
    println("String found");  // doesn't output
  }
}

 
When i manually input the value into the String Array, the if.. else statement picks it up just fine. Surely i've missed something completely obvious
 
Code:
void setup(){
  String myArray[] = new String[1];
  myArray[0] = "Hello";
   
  if (myArray[0] == "Hello") {  
    println("String found");  // outputs "String found"
  }
}

 
Any help greatly appreciated, ta for reading
 
whiteflea


Re: newbie loadStrings() question
« Reply #1 on: Sep 9th, 2004, 8:49am »

could it be, that Hello isnt stored in myArray[0]
 
its stored in:
myArray[0]=H
myArray[1]=e
myArray[2]=l
myArray[3]=l
myArray[4]=o
 
just a guess, im a newbie, too
 
cheers
whiteflea
 



==================================
Software is mindcontrol - get some
==================================
Shut up or I will replace you with
a very small processing script


TomC

WWW
Re: newbie loadStrings() question
« Reply #2 on: Sep 9th, 2004, 12:32pm »

Not quite, whiteflea.
 
== in Java checks whether two objects are identical, ie the same object.
 
To do what Robin expects, you should use the equals method of the String class, which compares the contents of the objects, not the objects themselves:
 
Code:

if (myArray[0].equals("Hello")) {    
  println("String found");  
}  

 
If that doesn't work then you should also check that it's not myArray[1] which actually contains "Hello".  Printing the length of myArray might also help you debug this one.
 
 
Robin


Re: newbie loadStrings() question
« Reply #3 on: Sep 9th, 2004, 3:29pm »

Thanks TomC, that solved it. Hmmm.. learning how to program in Actionscript has given me all sorts of nasty habits and misconceptions
 
Pages: 1 

« Previous topic | Next topic »