Link to home
Create AccountLog in
Java

Java

--

Questions

--

Followers

Top Experts

Avatar of Greging
Greging

What am I'm missing from this program?
Greetings.

Can anyone tell me what I'm missing from this program....I almost got it to work, until I got an error about missing a "{".
------------------------------
import java.io.*;
import java.util.*;

public class FilmSorter
{    
    public static void main (String[]args) throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("c:\\movielist.txt"));
        StringBuffer sb = new StringBuffer();
        Vector vec = new Vector();
        String temp = "";
        int t;
        boolean done = false;
        Movie mov = null;
        String tempBig = "";
        int mark = 0;
        int endMark = 0;
        while((t=br.read())!=32){
            mov=new Movie();
            temp+=(char)t;
    while(!done)
    {
        t=br.read();
        if(Character.isDigit((char)t))
        {
            done=true;
            mov.setName(temp);
            temp="";
        }
        else {temp+=(char)t;}
    }
    done=false;
    temp+=(char)t;
   
    while(!done)
    {
        t=br.read();
        if (Character.isWhitespace((char)t))
        {
            done=true;
            mov.setTime(temp);
            temp="";
        }
        else {temp+=(char)t;}
    }
    done=false;
   
    while(!done)
    {
        t=br.read();
        if(Character.isDigit((char)t))
        {
            done=true;
            mov.setActor(temp);
            temp="";
        }
        else{temp+=(char)t;}
    }
    done=false;
    temp+=(char)t;
   
    while(!done)
    {
        t=br.read();
        if (Character.isWhitespace((char)t))
        {
            done=true;
            mov.setYear(temp);
            temp="";
        }
        else{temp+=(char)t;}
    }
    done=false;
    vec.add(mov);
}
br.close();

Set set = new TreeSet();
for(int i=0; i < vec.size();i++)
{
    mov = (Movie)vec.get(i);
    set.add(mov.getName());
}

Iterator itr = set.iterator();
Vector sortedVec = new Vector();

while(itr.hasNext())
{
    temp = (String)itr.next();
   
    for(int i=0;i < vec.size();i++)
    {
        mov = (Movie)vec.get(i);
        if (temp.equals(mov.getName()))
        {
            sortedVec.add(mov);
        }
    }
        for(int i=0;i < sortedVec.size();i++)
        {
            mov = (Movie)sortedVec.get(i);
            System.out.println("Name  :  "+mov.getName()+"\nMovie length  : "+mov.getTime()+"\nWho is playing  :  "+mov.getActor()+"nYear :   "+mov.getYear());
        }
    }    

    class Movie
    {    
        private String name;
        private String time;
        private String actor;
        private String year;
   
        public String getName(){
            return name;
        }
   
        public String getTime(){
            return time;
        }
   
        public String getActor(){
            return actor;
        }
   
        public String getYear(){
            return year;
        }
   
        public void setName(String str){
            name = str;
        }
   
        public void setTime(String str){
            time = str;
        }
   
        public void setActor(String str){
            actor = str;
        }
   
        public void setYear(String str){
            year = str;
        }
    }
}
------------------------------------

Basically, what I'm trying to do is creating  program so that  it can scan on my .txt, which contain a movie list of 6 film records;
I'm trying to sort them in ascending order (by name or by duration/time). It's similiar to what MozillaFan told you guys about couple of days ago; I read it, and decided to take the shot at it. Here's a small sample of what it's look like in my text file:

Searching for Bobby Fischer
110
Max Pomeranc
1993
Shrek
89
Mike Myers
2001
Saving Private Ryan
170
Tom Hanks
1998  


         
   

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of asswaspasswasp

I'm not 100% sure (can you post the error message?), but I'm pretty sure you need to move the Movie class outside of the FilmSorter class.

(Remove the very last '}' and put it before 'class Movie' so it closes the FilmSorter class.)

class FilmSorter
{
   public static void main(String[] args)
   {
      //...your code
   }
}

class Movie
{
   //...methods
}

I think you would have a much easier time if you used the readLine() method of BufferedReader, and read the text file line by line instead of character by character.  Also make the year and the durations integers instead of Strings so that is is easy to sort with them.

java.util.LinkedList theList = new java.util.LinkedList();
String theMovie;
int theDuration;
String theActor;
int theYear;
Movie movie;
while((theMovie = br.readLine()) != null) {
theDuration = Integer.parseInt(br.readLine());
theActor = br.readLine();
theYear = Integer.parseInt(br.readLine());
movie = new Movie();
movie.setName(theMovie);
movie.setTime(theDuration);
movie.setActor(theActor);
movie.setYear(theYear);
theList.add(movie);
}



Avatar of GregingGreging

ASKER

Asswasp, I tried putting the last "}" right before "class Movie", but it won't work.

Iwinkenb, can you rewrite entire that I posted above so that I could get a better picture of your solution to my problem?

Thanks. :)...........

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of GregingGreging

ASKER

code I mean, oops......

Well use your same Movie class, except change the date, and the duration values from strings to integers.
Then, use my code in place of your code for the reading of the text file, and the creation of the movie objects.

BufferedReader br = new BufferedReader(new FileReader("c:\\movielist.txt"));
java.util.LinkedList theList = new java.util.LinkedList();
String theMovie;
int theDuration;
String theActor;
int theYear;
Movie movie;

//Read the file until there are no more movies
while((theMovie = br.readLine()) != null) {
theDuration = Integer.parseInt(br.readLine());
theActor = br.readLine();
theYear = Integer.parseInt(br.readLine());
movie = new Movie();
movie.setName(theMovie);
movie.setTime(theDuration);
movie.setActor(theActor);
movie.setYear(theYear);
theList.add(movie);
}

Then to sort, you would just need to use the linkedlist in 2 nested for loops, or you could load
the values in an array and sort from there using a faster algorithm like quicksort.

Avatar of GregingGreging

ASKER

Ok, you mean like this:
----------------------------
import java.io.*;
import java.util.*;

public class FilmSorter
{    
    public static void main (String[]args) throws IOException
    {
    BufferedReader br = new BufferedReader(new FileReader("c:\\movielist.txt"));
        StringBuffer sb = new StringBuffer();
        java.util.LinkedList theList = new java.util.LinkedList();
      String theMovie;
      int theDuration;
      String theActor;
      int theYear;
      Movie movie;

      while((theMovie = br.readLine()) != null)
      {
            theDuration = Integer.parseInt(br.readLine());
            theActor = br.readLine();
            theYear = Integer.parseInt(br.readLine());
            movie = new Movie();
            movie.setName(theMovie);
            movie.setTime(theDuration);
            movie.setActor(theActor);
            movie.setYear(theYear);
            theList.add(movie);
      }
...............

...............

    class Movie
    {    
        private String name;
        private Integers time;
        private String actor;
        private Integers year;
   
        public String getName(){
            return name;
        }
   
        public Integers getTime(){
            return time;
        }
   
        public String getActor(){
            return actor;
        }
   
        public Integers getYear(){
            return year;
        }
   
        public void setName(String str){
            name = str;
        }
   
        public void setTime(Integers int){
            time = int;
        }
   
        public void setActor(String str){
            actor = str;
        }
   
        public void setYear(Integers int){
            year = int;
        }
    }
}
--------------------------------------        
I can't think of what to fill in for the middle part of the codes. (Keep in mind that I'm still  new to working with Java though)


Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of lwinkenblwinkenb

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of GregingGreging

ASKER

I have an alternative program I want to show you. But I think this is better than the one I show above, but still got errors. Any solutions...?
---------------------------------------------
&#65279;import java.io.*;
import java.util.*;
public class Sorter {
      protected String data[];
      public static void main(String[] args) {
            try {
                  if (args.length >= 2) {
                        new Sorter(args[0],args[1]);
                  }
                  else {
                        System.out.println("Bad parameters.");
                        System.out.println("Usage: ");
                        System.out.println("java Sorter c://movielist.txt ");
                        System.out.println("Modes: ");
                        System.out.println("-name to sort by name.");
                        System.out.println("-time to sort by time.");
                  }
            }
            catch (Exception e) {
                  System.err.println("Error: "+e.toString());
                  System.exit(1);
            }
            finally {
                  System.exit(0);
            }
      }
      public Sorter(String mode,String file) throws IOException {
            int j;
            Vector v = new Vector(0);
            BufferedReader b = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
            String s;
            while ((s = b.readLine()) != null) {
                  v.add(new String(s));
            }
            Object tmp[] = v.toArray();
            data = new String[tmp.length];
            for (j = 0;j < tmp.length;j++) {
                  data[j] = (String)tmp[j];
            }
            String result[] = (mode.equals("-time")?sortByTime():sortByName());
            for (j = 0;j < result.length;j++) {
                  System.out.println(result[j]);
            }
      }
      protected String[] sortByName() {
            String names[],sets[];
            int j,n,r;
            names = new String[data.length/4];
            for (j = 0;j < names.length;j++) {
                        names[j] = data[j*4];
            }
            Arrays.sort(names);
            sets = new String[data.length];
            for (j = 0;j < names.length;j++) {
                  sets[j*4] = names[j];
                  n = 0;
                  while (!data[n].equals(names[j])) n++;
                  for (r = 1;r <= 3;r++) {
                        sets[j*4+r] = data[++n];
                  }
            }
            return sets;
      }
      protected String[] sortByTime() {
            return new String[] {"blank"};
      }
}
---------------------------------

Avatar of TimYatesTimYates🇬🇧

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-- Points for lwinkenb

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
Java

Java

--

Questions

--

Followers

Top Experts

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.