Sapperm's Profile User Rating: -----

Reputation: 2 Apprentice
Group:
New Members
Active Posts:
15 (0.01 per day)
Joined:
31-October 12
Profile Views:
325
Last Active:
User is offline Sep 18 2013 08:36 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   Sapperm has not set their status

Posts I've Made

  1. In Topic: Reading data from an array into an object

    Posted 16 Sep 2013

    Well I worked more on it as soon as I posted the question. I think I found a valid solution by using array lists.
    *I made an array list
    *populated the list
    *converted the list to a int array via loop
    *sent in the array to the constructor
    *used .clear to clear the list and start over

    import java.util.*;
    import java.io.*;
    /**
     * 
     * @author 
     *
     */
    public class Lab2 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		Grades[] classArray;
    	     classArray = new Grades[4];
    	
    	     ArrayList<Integer> gradeList = new ArrayList<Integer>();
    		
    	     
    	  
    		int [] grades;
    		grades = new int[1];
    		
    		String name;
    		  
            int counter = 0;
    		
            
    		Scanner in = null;
    		try {
    		    in = new Scanner(new File("data.txt"));
    		} catch (FileNotFoundException exception) {
    		    System.err.println("failed to open data.txt");
    		    System.exit(1);
    		}
    		int j = 0;
    		while (in.hasNext()) {
    			name = in.next();
    			
    		
    		    while (in.hasNextInt()) {
    		    	int grade = in.nextInt();
    			  
    		    	gradeList.add(grade);
    				
    		//		if (counter == grades.length) {
    			//		
    			//		grades = Arrays.copyOf(grades, 2 * grades.length);
    		//		}
    				
    				
    				
    			//	grades[counter] = grade;
    				counter++;
    	
    		    }
    		 
    		   
    		    grades = new int[gradeList.size()];
    		    for (int index = 0; index < gradeList.size(); index++) {
    		        grades[index] = gradeList.get(index);
    		    }
    		   // System.out.println("gradelist.size = "+ gradeList.size());
    		   // grades = Arrays.copyOf(grades, counter);
    		   
    		    
    		    classArray[j] = new Grades(name,grades);
    		    gradeList.clear();
    		 
    		    j++;
    		 
    		}
    		//grades = Arrays.copyOf(grades, counter);
    //		Arrays.sort(grades);
    	/*
    		for(int i = 0; i < counter; i++){
    		    
    			System.out.printf("counter = %d\n",grades[i]);
    		
    		}
    		*/
    
    		testGrades(classArray[0]);
    		testGrades(classArray[1]);
    		testGrades(classArray[2]);
    		testGrades(classArray[3]);
    	}//end main
    	
    	public static void testGrades(Grades grades) {
    	    System.out.println(grades.toString()); 
    	    System.out.printf("\tName:    %s\n", grades.getName());
    	    System.out.printf("\tLength:  %d\n", grades.length());
    	    System.out.printf("\tAverage: %.2f\n", grades.average());
    	    System.out.printf("\tMedian:  %.1f\n", grades.median());
    	    System.out.printf("\tMaximum: %d\n", grades.maximum());
    	    System.out.printf("\tMininum: %d\n", grades.minimum());
    	}
    
    }//end class
    
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

Sapperm hasn't added any friends yet.

Comments

Sapperm has no profile comments yet. Why not say hello?