ILoveJava's Profile User Rating: -----

Reputation: 29 Craftsman
Group:
Active Members
Active Posts:
391 (0.26 per day)
Joined:
12-March 12
Profile Views:
7,440
Last Active:
User is offline Jun 15 2014 03:40 PM
Currently:
Offline

Previous Fields

Country:
AU
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
0

Latest Visitors

Icon   ILoveJava Java <3

Posts I've Made

  1. In Topic: Printing altered arraylist

    Posted 7 Jun 2014

    Bump for a very frustrated student.
  2. In Topic: Printing altered arraylist

    Posted 5 Jun 2014

    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();
            }
        }
    }
    
  3. In Topic: Printing altered arraylist

    Posted 29 May 2014

    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?

My Information

Member Title:
D.I.C Regular
Age:
21 years old
Birthday:
August 16, 1994
Gender:
Location:
Australia
Full Name:
Nathan Kreider
Years Programming:
1
Programming Languages:
Theoretical Programming

Contact Information

E-mail:
Private

Comments

Page 1 of 1
  1. Photo

    AnalyticLunatic Icon

    30 Aug 2012 - 13:17
    Professional Chef, eh? Have any recipes worth sharing?
Page 1 of 1