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.tx t");
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(aNextLi ne);
Movie aMovie = new Movie (year, title);
arrMovies[i] = aMovie;
System.out.println(arrMovi es[i].gety ear() + " - " + 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("Fichie r introuvable!");
}
}
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.tx
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(aNextLi
Movie aMovie = new Movie (year, title);
arrMovies[i] = aMovie;
System.out.println(arrMovi
StringTokenizer st3 = new StringTokenizer(aLine, ",");
noActor = st3.nextToken();
System.out.println(noActor
i++;
}
br.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Fichie
}
}
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
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 );
}
while (st3.hasMoreTokens())
{
noActor = st3.nextToken();
System.out.println(noActor
}
ASKER
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(arr Actors[j], index);
index++;
}
}
}
// From the Actor class
String actorNumber;
public String getNumber()
{
return actorNumber;
}
Thanks a lot for the help !
Frank
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(arr
index++;
}
}
}
// From the Actor class
String actorNumber;
public String getNumber()
{
return actorNumber;
}
Thanks a lot for the help !
Frank
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That just did my night ! All my comparison where like that ;)
Thanks a lot objects !!
Frank
Thanks a lot objects !!
Frank
no worries :)
that should be
aLine = br.readLine();