?
Solved

StringTokenizer: How to handle multiple lines

Posted on 2005-03-13
8
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 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