Link to home
Start Free TrialLog in
Avatar of The_Kingpin08
The_Kingpin08

asked on

StringTokenizer: How to handle multiple lines

Hi again all,

I'm still working on a StringTokenizer manipulation project and I have more questions for you specialists =)
II'm trying to use StringTokenizer to handle multiple line. Here's precisely what I'm trying to do:

I have a file containing on the first line a year and a movie title. On the next line, I have a couple of numbers separated by "," which represent actor's number.
Here's a sample:

1955 The night of the hunter
02, 21, 10, 46

I'd like to have a variable that contain each of these infromations, but don't really know how to do it using StringTokenizer... I tred anyway and here's what it looks like so far...

        public static void InitArrMovie(Movie[] arrMovies, Actor[] arrActors)
      {
            String aLine, aNextLine, year = "", title = "", noActor = "";
            int i = 0;
            
            try
            {
                  FileInputStream fin =  new FileInputStream("Movies.txt");
                  BufferedReader br = new BufferedReader(new InputStreamReader(fin));
                  
                  while ((aLine = br.readLine()) != null)
                  {  
                        StringTokenizer st2 = new StringTokenizer(aLine);
                        year = st2.nextToken();                        
                        title = st2.nextToken("\n\r");
                        
                        aNextLine = st2.nextToken("\n\r");
                        System.out.println(aNextLine);
                        
                        
                        Movie aMovie = new Movie (year, title);
                        arrMovies[i] = aMovie;
                        System.out.println(arrMovies[i].getyear() + " - " + arrMovies[i].getTitle());
                        
                        StringTokenizer st3 = new StringTokenizer(aLine, ",");
                        noActor = st3.nextToken();
                        System.out.println(noActor);
                        
                        i++;
                  }
                  
            br.close();  
            }
   
            catch (Exception e) {
                  e.printStackTrace();
                  System.out.println("Fichier introuvable!");
            }
      }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>                     aNextLine = st2.nextToken("\n\r");

that should be

aLine = br.readLine();

Avatar of The_Kingpin08
The_Kingpin08

ASKER

Thanks objects,

now that I can read the second line, how do I divide it into tokens so I can read every numbers (separated by ",") ? I don't know on which condition I need to build my loop - how do I check if it's the end of the line ?

Thanks,
Frank
                   StringTokenizer st3 = new StringTokenizer(aLine, ",");
                    while (st3.hasMoreTokens())
                    {
                       noActor = st3.nextToken();
                       System.out.println(noActor);
                    }

example of how to convert an int at:

http://www.objects.com.au/java/conversions.do
There's something I don't understand... I've convert the value of a token into a string so I can compare it to an object's property, but the comparison always returns me false, even if the printed value are the same ?!?! Here's the code sample I use to compare:

aLine = br.readLine();
StringTokenizer st3 = new StringTokenizer(aLine, ",");
while (st3.hasMoreTokens())
{
      actorNo = st3.nextToken();
      for (j = 0; j <= arrActors.length; j++)
      {
                // This is the comparison that doesn't word <--------------------------------
            if (actorNo == arrActors[j].getNumber())
            {
                  System.out.println("Actor Found!");
                  arrMovies[i].AddActors(arrActors[j], index);
                  index++;
            }
      }
}

// From the Actor class
String actorNumber;
      
public String getNumber()
{
      return actorNumber;
}


Thanks a lot for the help !
Frank
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That just did my night ! All my comparison where like that ;)

Thanks a lot objects !!

Frank
no worries :)