Java
--
Questions
--
Followers
Top Experts
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.
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
{
done=true;
mov.setName(temp);
temp="";
}
else {temp+=(char)t;}
}
done=false;
temp+=(char)t;
while(!done)
{
t=br.read();
if (Character.isWhitespace((c
{
done=true;
mov.setTime(temp);
temp="";
}
else {temp+=(char)t;}
}
done=false;
while(!done)
{
t=br.read();
if(Character.isDigit((char
{
done=true;
mov.setActor(temp);
temp="";
}
else{temp+=(char)t;}
}
done=false;
temp+=(char)t;
while(!done)
{
t=br.read();
if (Character.isWhitespace((c
{
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.
(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
}
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.readLi
theActor = br.readLine();
theYear = Integer.parseInt(br.readLi
movie = new Movie();
movie.setName(theMovie);
movie.setTime(theDuration)
movie.setActor(theActor);
movie.setYear(theYear);
theList.add(movie);
}
Iwinkenb, can you rewrite entire that I posted above so that I could get a better picture of your solution to my problem?
Thanks. :)...........






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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.
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.readLi
theActor = br.readLine();
theYear = Integer.parseInt(br.readLi
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.
--------------------------
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.
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.readLi
theActor = br.readLine();
theYear = Integer.parseInt(br.readLi
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)

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.
--------------------------
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:
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")?sort
for (j = 0;j < result.length;j++) {
System.out.println(result[
}
}
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])
for (r = 1;r <= 3;r++) {
sets[j*4+r] = data[++n];
}
}
return sets;
}
protected String[] sortByTime() {
return new String[] {"blank"};
}
}
--------------------------
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
--
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.