?
Solved

StringTokenizer: How to handle multiple lines

Posted on 2005-03-13
8
Medium Priority
?
460 Views
Last Modified: 2006-11-17
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!");
            }
      }
0
Comment
Question by:The_Kingpin08
  • 5
  • 3
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13531747
>                     aNextLine = st2.nextToken("\n\r");

that should be

aLine = br.readLine();

0
 

Author Comment

by:The_Kingpin08
ID: 13531780
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
0
 
LVL 92

Expert Comment

by:objects
ID: 13531825
                   StringTokenizer st3 = new StringTokenizer(aLine, ",");
                    while (st3.hasMoreTokens())
                    {
                       noActor = st3.nextToken();
                       System.out.println(noActor);
                    }

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 92

Expert Comment

by:objects
ID: 13531829
example of how to convert an int at:

http://www.objects.com.au/java/conversions.do
0
 

Author Comment

by:The_Kingpin08
ID: 13531935
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
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13531943
> if (actorNo == arrActors[j].getNumber())

== test's whether they are the same object. To test if the values are the same use the equals() method

if (actorNo.equals(arrActors[j].getNumber()))

0
 

Author Comment

by:The_Kingpin08
ID: 13531957
That just did my night ! All my comparison where like that ;)

Thanks a lot objects !!

Frank
0
 
LVL 92

Expert Comment

by:objects
ID: 13532452
no worries :)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month4 days, 7 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question