ArrayList


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: ArrayList

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Posts
    10

    Question ArrayList

    I'm supposed to complete computeStudentMean method and the displayMeans method. Method computeStudentMean accesses the four grades in each individual student record, computes the mean of the four grades and stores the result in mean. Method displayMeans list all the student name and their respective test averages.
    My problem is that I keep on getting an error that says I have a ClassCastException
    Code:
    import java.io.*; 
    import java.util.ArrayList; 
        
      
      
    public class lab20 
    { 
       public static void main(String args[]) throws IOException 
       { 
          System.out.println("Lab 20 80/100 POINT VERSION"); 
          ClassList list = new ClassList(); 
          list.getList(); 
          list.pause(); 
          list.computeStudentMean(); 
    //      list.computeTestMean(); 
    //      list.computeClassMean(); 
          list.displayMeans();       
          System.out.println(); 
       } 
    } 
      
      
    class ClassList 
    {    
      
       class Student 
       { 
          private String name; 
          private ArrayList grades; 
          private double mean; 
        
          public Student(String n, int g1, int g2, int g3, int g4) 
          { 
             name = n; 
             grades = new ArrayList(); 
             grades.add(new Integer(g1)); 
             grades.add(new Integer(g2)); 
             grades.add(new Integer(g3)); 
             grades.add(new Integer(g4)); 
          } 
       } 
      
       private ArrayList students; 
       private int studentCount;    
       private ArrayList testMean; 
       private int testCount; 
        private double classMean; 
        
       public ClassList() 
       { 
          students = new ArrayList();  
          testMean = new ArrayList(); 
          studentCount = 0; 
          testCount = 4;          
       } 
      
       public void getList() throws IOException 
       { 
          System.out.println("\nRetrieving Grades.dat\n"); 
          FileReader inFile = new FileReader("Grades.dat");    
          BufferedReader inStream = new BufferedReader(inFile);         
          String s1,s2,s3,s4,s5; 
          while(    ((s1 = inStream.readLine()) != null) && 
                ((s2 = inStream.readLine()) != null) && 
                ((s3 = inStream.readLine()) != null) && 
                ((s4 = inStream.readLine()) != null) && 
                ((s5 = inStream.readLine()) != null)   ) 
          { 
             System.out.println(s1 + "\t\t" + s2 + "\t" + s3 + "\t" + s4 + "\t" + s5); 
             String name = s1; 
             int g1 = Integer.parseInt(s2); 
             int g2 = Integer.parseInt(s3); 
             int g3 = Integer.parseInt(s4); 
             int g4 = Integer.parseInt(s5); 
             students.add(new Student(name,g1,g2,g3,g4));        
          }    
          inStream.close();                   
          studentCount = students.size(); 
       } 
       public void pause() throws IOException 
       {    
          BufferedReader input = new BufferedReader(new InputStreamReader(System.in));    
          String dummy; 
          System.out.print("\nPress <Enter> to continue  ===>>  ");                   
          dummy = input.readLine();                         
       } 
        
        
       public void displayData() 
       { 
          System.out.println("\nDISPLAYING STUDENT DATA"); 
       }    
        
       public void computeStudentMean() 
       { 
          int sum=0; 
          for(int k=0; k<studentCount; k++) 
          { 
          Integer temp = (Integer) students.get(k); <--ERROR OCCURS HERE 
          sum += temp.intValue(); 
      
          } 
       double mean =(double)sum/4; 
       testMean.add(new Double(mean));    
       } 
        
        
       public void computeTestMean() 
       {    
       } 
        
        
       public void computeClassMean() 
       {    
       } 
      
      
         public void displayMeans() 
       { 
       System.out.println("\nDISPLAYING MEAN STATISTICS"); 
          for(int k=0; k><testMean.size(); k++) 
                System.out.println((Double) testMean.get(k)); 
       }    
      
    }
    Am I getting this error because my code is completely wrong, or is it that I accessed a wrong record, and if so, how can I correct it? Thank you.
    Absit invidia

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    Code:
    Integer temp = (Integer) students.get(k); <--ERROR OCCURS HERE
    The students is an ArrayList of Student objects and not Integer objects. Casting will always generate a runtime exception here.
    Happiness is good health and a bad memory.

Similar Threads

  1. Replies: 1
    Last Post: 12-24-2005, 07:12 AM
  2. Storing ArrayList from ResultSet
    By lamagra in forum Java
    Replies: 1
    Last Post: 06-29-2005, 10:08 PM
  3. Replies: 10
    Last Post: 05-15-2005, 07:14 AM
  4. Replies: 4
    Last Post: 04-13-2005, 10:37 PM
  5. Converting arraylist to array
    By warriorwarren in forum Java
    Replies: 11
    Last Post: 03-19-2005, 02:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center