6 Replies - 536 Views - Last Post: 08 June 2014 - 01:41 AM Rate Topic: -----

#1 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Printing altered arraylist

Posted 29 May 2014 - 07:10 PM

Yes, this is an assessment I am currently doing. No, I am not asking for the code, I am asking for guidance on how to fix my problem

I apologize for the wall of text.

Hey guys, so I'm doing an assessment, and we're required to create a small Student program. One class is just a constructor class that the objects will pass through, the other is the main class.

Part of the assessment is to read in the student data from a file, store it in an arraylist, then bubble sort it, list id's lowest to highest. This I have done, and believe it is working correctly.
The next part is to re-sort the ArrayList, ordered by the student id, like it was done in the bubblesort method. This is the part I'm stuck on, I've tried setting the id's in the arraylist via the bubbleSort method, but I'm just having no luck. Any help would be greatly appreciated on this.
Main class.
package assessment.pkg3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
 *
 * @author Nathan
 */
public class Assessment3 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, InputMismatchException {    
        // Initialize instance variables
        String surname;
        String givenNames;
        String prefName;
        int studentId;
        boolean fullTime;
        String contactNumber;
        boolean loveJava;
        String diploma;  
        
        // Small integer array for holding student id's.
        int[] idList = new int[22];
        
        // Initialize ArrayList<Students>
        ArrayList<Students> studentsList = new ArrayList<Students>();   
        
        File inFile = new File("students.txt");
        Scanner sc = new Scanner(inFile);
        sc.useDelimiter(",");
        
        while(sc.hasNext()) {
            surname = sc.next();
            givenNames = sc.next();
            prefName = sc.next();
            studentId = sc.nextInt();
            fullTime = sc.nextBoolean();
            contactNumber = sc.next();
            loveJava = sc.nextBoolean();
            diploma = sc.next();
            //studentsList.add(new Students(scanner.next(), scanner.next(), scanner.next(), scanner.nextInt(), scanner.nextBoolean(), scanner.nextFloat(), scanner.nextBoolean(), scanner.next()));
            studentsList.add(new Students(surname, givenNames, prefName, studentId, fullTime, contactNumber, loveJava, diploma));
        }
        //sc.close();            
        System.out.println("/////// Unformatted output");
        for(int i = 0; i < studentsList.size(); i++) {
            Students s = studentsList.get(i);
            idList[i] = s.getStudentId();

            System.out.println(idList[i]);
        }            
        bubbleSort(idList);
    }
    
    // bubbleSort method for sorting lowest to highest.
    public static void bubbleSort(int[] array) {
        // Declare instance variables
        int oCounter;
        int iCounter;
        int element;

        for(oCounter = 0; oCounter < array.length - 1; oCounter++) {
            for(iCounter = 0; iCounter < array.length - 1 - oCounter; iCounter++) {
                if(array[iCounter] > array[iCounter + 1]){
                    element = array[iCounter];
                    array[iCounter] = array[iCounter + 1];
                    array[iCounter + 1] = element;
                    //array[iCounter].setStudentId(element);
                    //System.out.println(array[iCounter]);
                }
            }
        }
        // Displays data post-formatting.
        System.out.println("/////// Formatted output");
        for(int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}

Students object class if it is required.
Spoiler

This post has been edited by ILoveJava: 29 May 2014 - 07:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Printing altered arraylist

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing altered arraylist

Posted 29 May 2014 - 07:39 PM

You will need two bubbleSort() methods each one using its own sort criteria
or a single method that receives as parameter the array an a flag specifying, in a way or another, the sort criteria
Was This Post Helpful? 0
  • +
  • -

#3 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Printing altered arraylist

Posted 29 May 2014 - 09:13 PM

On closer inspection, I've misread the project brief. Instead of parsing just the student id into the bubble sort, I need to parse the entire arraylist, then sort it all by the studentid, then print it to a text file.
I've yet to work out the rest of it, but i've changed the bubbleSort method to
public static void bubbleSort(Students[] array) {

but now I'm getting errors on some of my if statements as below.
                if(array[iCounter + 1] > array[iCounter]){
                    element = array[iCounter];
                    array[iCounter] = array[iCounter + 1];
                    array[iCounter + 1] = element;


NetBeans is just giving me "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: assessment.pkg3.Assessment3.bubbleSort
at assessment.pkg3.Assessment3.main(Assessment3.java:57)" as the error.

Has anyone had to do this kind of thing before?

This post has been edited by ILoveJava: 29 May 2014 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,370
  • Joined: 20-September 08

Re: Printing altered arraylist

Posted 30 May 2014 - 02:08 AM

Quote

: Uncompilable source code - Erroneous sym type: assessment.pkg3.Assessment3.bubbleSort
at assessment.pkg3.Assessment3.main(Assessment3.java:57)" as the error.

And line 57 is what exactly?
Was This Post Helpful? 0
  • +
  • -

#5 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Printing altered arraylist

Posted 05 June 2014 - 06:27 PM

Okay, I worked out what was wrong with my code, and now everything is being sent down properly, and I think I've done what you suggested, pbl. In my bubbleSort method it takes a 'type' String, which then sends the data to it's respected switch case.
I'm having a very small issue with my bubbleSort method now that I've "fixed" it to accept the ArrayList data, that now it will only sort the first 2 rows in the ArrayList data. I'm not really sure what I can do to fix this.
package assessment.pkg3;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
 *
 * @author Nathan
 */
public class Assessment3 {
    /**
     * @param args the command line arguments
     */   
    
    // Initialize ArrayList<Students>
    static ArrayList<Students> studentsList = new ArrayList<Students>();  
    static ArrayList<Students> studentsBySurname = new ArrayList<Students>();
    static ArrayList<Students> studentsById = new ArrayList<Students>();
    
    public static void main(String[] args) throws FileNotFoundException, InputMismatchException, IOException {    
        // Initialize instance variables
        String surname;
        String givenNames;
        String prefName;
        int studentId;
        boolean fullTime;
        String contactNumber;
        boolean loveJava;
        String diploma;
        
        // Declare file variables for handling reading in, and printing out.
        File inFile = new File("students.txt");
        File idFile = new File("byId.txt");
        File surnameFile = new File("bySurname.txt");
        
        FileWriter surnameOut = new FileWriter(surnameFile);
        FileWriter idOut = new FileWriter(idFile);
        
        Scanner sc = new Scanner(inFile);
        sc.useDelimiter(",");
        
        while(sc.hasNext()) {
            surname = sc.next();
            givenNames = sc.next();
            prefName = sc.next();
            studentId = sc.nextInt();
            fullTime = sc.nextBoolean();
            contactNumber = sc.next();
            loveJava = sc.nextBoolean();
            diploma = sc.next();
            studentsList.add(new Students(surname.trim(), givenNames, prefName, studentId, fullTime, contactNumber, loveJava, diploma));
            studentsBySurname.add(new Students(surname.trim(), givenNames, prefName, studentId, fullTime, contactNumber, loveJava, diploma));
            studentsById.add(new Students(surname.trim(), givenNames, prefName, studentId, fullTime, contactNumber, loveJava, diploma));
        }
        sc.close();            
        // Parse studentList arrayList to the bubbleSort method
        bubbleSort(studentsList, "surname");
        bubbleSort(studentsList, "id");   
        
        // Print original ArrayList
        System.out.println("Original");
        for(int i = 0; i < studentsList.size(); i++) {
            System.out.println(studentsList.get(i).toString());
        }
        
        // Print contents of the sorted ArrayList's
        System.out.println("Surname");
        for (int i = 0; i < studentsBySurname.size(); i++) {
            System.out.println(studentsBySurname.get(i).toString());
            surnameOut.write(studentsBySurname.get(i).toString() + "\n");
        }
        System.out.println("id");       
        for (int i = 0; i < studentsById.size(); i++) {
            System.out.println(studentsById.get(i).toString());
            idOut.write(studentsById.get(i).toString() + "\n");
        }
        surnameOut.close();
        idOut.close();
    }
    
    // bubbleSort method for sorting ArrayList data sent down.
	// case "id" will sort the data by the studentId variable.
	// case "surname" will sort the data by the surname variable.
    public static void bubbleSort(ArrayList<Students> array, String type) throws IOException {
        // Declare instance variables
        int oCounter;
        int iCounter;
        Students element;
        
        try {
            switch(type) {
                case "id":
                    for(oCounter = 0; oCounter < array.size() - 1; oCounter++) {
                        for(iCounter = 0; iCounter < array.size() - 1 - oCounter; iCounter++) {
                            if(array.get(iCounter).getStudentId() < array.get(iCounter+1).getStudentId()){
                                element = array.get(iCounter);
                                studentsById.set(iCounter, array.get(iCounter + 1));
                                studentsById.set(iCounter + 1, element);
                            }
                        }
                    }
                break;

                case "surname":
                    for(oCounter = 0; oCounter < array.size() - 1; oCounter++) {
                        for(iCounter = 0; iCounter < array.size() - 1 - oCounter; iCounter++) {
                            if(array.get(iCounter).getSurname().compareToIgnoreCase(array.get(iCounter+1).getSurname()) > 0) { 
                                element = array.get(iCounter);
                                studentsBySurname.set(iCounter, array.get(iCounter + 1));
                                studentsBySurname.set(iCounter + 1, element);
                            }
                        }
                    }
                break;
            }
        } catch(IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Printing altered arraylist

Posted 07 June 2014 - 07:46 PM

Bump for a very frustrated student.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,370
  • Joined: 20-September 08

Re: Printing altered arraylist

Posted 08 June 2014 - 01:41 AM

Quote

if (array.get(iCounter).getStudentId() < array.get(iCounter + 1).getStudentId()) 
And what about if it's greater than? Then it won't be sorted. Your criterion should be equality

To make your code more readable you could call this method in both of your cases:

    public static <T extends Comparable<T>> boolean areEqual(T c1, T c2) {
	return c1.compareTo(c2) == 0;
    }


This post has been edited by g00se: 08 June 2014 - 01:54 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1