Linked List problem

read date from text file into linked list

Page 1 of 1

8 Replies - 5229 Views - Last Post: 20 May 2010 - 10:58 PM Rate Topic: -----

#1 samialibi  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 8
  • Joined: 13-February 10

Linked List problem

Posted 20 May 2010 - 09:35 PM

Hi all;
I have problem when I have tried read information from text file called(Movie.dat)into linked list of Film objects
this is File Movie,dat: contains,the number of film records,followed by the film records of the form:
title
genre
screening
attendance
a partial listing of an example of Movies.dat as shown below:


The Saumrai
Actions
222
222222

.


and this is the Film class

public class Film
{
   private String title;
   private String genre;
   private int screenings;
   private int attendance;

   public Film(String title,String genre,int screenings,int attendance)
   {
      this.title = title;
      this.genre = genre;
      this.screenings = screenings;
      this.attendance = attendance;
   }
    public String toString()
    {
       return "title" + title + "genre" + screenings + "attendance" +attendance;
    }

    public String getTitle()
    {
       return title;
    }

    public String getGenre()
    {
       return genre;
    }

    public int getScreenings()
    {
       return screenings;
    }

    public int getAttendance()
    {
       return attendance;
    }

    public void setScreenings(int s)
    {
       this.screenings = s;
    }

    public void setAttendance(int a)
    {
       this.attendance = a;
    }

    public double calcaerage()
    {
       if(screenings == 0 || attendance == 0)
       {
          return -1;
       }
       else
       {
          double avr = attendance/screenings;
          return avr;
       }
    }
}


and this is CinemaDriver clss which I have to read data from the text file into likedlist of film objects

import java.util.*;
import java.io.*;
public class CinemaDriver
{
   public static void main(String[]args)throws IOException
   {
      Film f = null;
      LinkedList<Film>  f1 = new LinkedList<Film>();
     // Scanner inputStream = null;

      
      
       Scanner  inputStream = new Scanner(new File("Movies.dat"));
         String title = inputStream.nextLine();
         String genre = inputStream.nextLine();
         int screenings = inputStream.nextInt();
         int attendance = inputStream.nextInt();
         f = new Film(title,genre,screenings,attendance);
         f1.add(f);
      
   }
}


thank you

-Moderator Edit: Please, in the future: :code:

-Locke

This post has been edited by Locke: 20 May 2010 - 10:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Linked List problem

#2 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Linked List problem

Posted 20 May 2010 - 10:04 PM

You seem to have some contradictions here.

If the beginning of your movie.dat file starts with "6" then when you load up the movie.dat into the scanner, your first number drawn from movie.dat is the Title String. When you try to parseInt, you will be trying to parse a String. There is your error.

Hope I helped.
Was This Post Helpful? 0
  • +
  • -

#3 samialibi  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 8
  • Joined: 13-February 10

Re: Linked List problem

Posted 20 May 2010 - 10:24 PM

View PostEmber, on 20 May 2010 - 09:04 PM, said:

You seem to have some contradictions here.

If the beginning of your movie.dat file starts with "6" then when you load up the movie.dat into the scanner, your first number drawn from movie.dat is the Title String. When you try to parseInt, you will be trying to parse a String. There is your error.

Hope I helped.


THE error is type linkedlist doesnt take parameters
LinkedList<Film> f1 = new LinkedList<Film> ();
Was This Post Helpful? -1
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Linked List problem

Posted 20 May 2010 - 10:30 PM

Seems like you might be using an old version of Java. If it doesn't take parameters inside the < >, then you're using version 1.4.2 or older. The generics came in Java 5.

Other than that...I can't see why this program wouldn't compile. It compiles when I tried it.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Linked List problem

Posted 20 May 2010 - 10:32 PM

View Postsamialibi, on 21 May 2010 - 12:24 AM, said:

THE error is type linkedlist doesnt take parameters
LinkedList<Film> f1 = new LinkedList<Film> ();



But it does...it's called type-parametrization, or generics...
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Linked List problem

Posted 20 May 2010 - 10:36 PM

View PostDogstopper, on 20 May 2010 - 11:32 PM, said:

But it does...


Not in JDK 1.4.2. :D

Us old farts were around for those days. ;)

So my high school used 1.4.2 when 5 was out? Big whoop, wanna fight about it?
Was This Post Helpful? 0
  • +
  • -

#7 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Linked List problem

Posted 20 May 2010 - 10:42 PM

Yea...I ran the code on the latest and greatest Java Compiler / VM and it works like a charm...

edit- How can a school not update their Java versions? It takes like 10 minutes..

This post has been edited by Ember: 20 May 2010 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#8 samialibi  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 8
  • Joined: 13-February 10

Re: Linked List problem

Posted 20 May 2010 - 10:46 PM

View PostEmber, on 20 May 2010 - 09:42 PM, said:

Yea...I ran the code on the latest and greatest Java Compiler / VM and it works like a charm...

edit- How can a school not update their Java versions? It takes like 10 minutes..

Thank you all
so there is no error in my code
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Linked List problem

Posted 20 May 2010 - 10:58 PM

View PostLocke, on 21 May 2010 - 12:36 AM, said:

View PostDogstopper, on 20 May 2010 - 11:32 PM, said:

But it does...


Not in JDK 1.4.2. :D

Us old farts were around for those days. ;)

So my high school used 1.4.2 when 5 was out? Big whoop, wanna fight about it?


oops...didn't mean that...oh well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1