Results 1 to 6 of 6
  1. #1
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default How do I call my methods correctly?

    (Updated: see latest post!)

    Hi,

    I have written a small program which is supposed to read in records of student marks stored in a text file. It is then supposed to print out the records, ordered alphabetically by surname.

    I have done most of it... see here:
    gillinghamband.org/java/Results.java

    but am struggling to work out how I can sort the objects in the array into alphabetical order by comparing the surname variable of each array object...

    ...so if anyone has any hints about how I can do that, it would be greatly appreciated! :-)

    ...also it would be good to get some general feedback about how my code could be improved... (I do feel like I've written a lot of code in order to achieve something that sounds like it should be fairly simple...!)

    Thanks,

    Lisa :)
    Last edited by lisalala; 03-03-2009 at 11:04 AM. Reason: new issue

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The easiest way to sort is to make sure your Student class (or whatever class you have that stores the Student's name together with his/her test scores) implements the Comparable<Student> interface. If you do this, then if your array is called students, simply use the Arrays.sort(students) method to sort the array. Your compareTo method will need to compare the surnames. The easiest way for it to do this is to simply use String's compareTo method.

  3. #3
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply. I'm still having some problems getting my code to work :-(

    Basically, everything is ok up until where I have added in the recordSort method - Here, the compiler complains lots about the fact that it can't see my RecordArray variable from within the method.
    I have tried placing the method in different places but with no luck so far... I am a bit stuck as to how I should restructure my code to make it compile...

    Also I am not entirely sure if I have written my compareTo method correctly...

    Any advice would be very much appreciated! (I will post my code below)

    Thanks :-)

  4. #4
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    import java.io.File;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.lang.NumberFormatException;
    import java.util.StringTokenizer;
    import java.util.Arrays;


    public class Results {

    Record[] RecordArray;

    static public class Record {
    char[] RecordInitials;
    String RecordSurname;
    int RecordMark;

    //method to set initials for record
    public void setInitials(char[] InitialsArray) {
    RecordInitials = InitialsArray;
    }

    //method to get initials for record
    public char[] getInitials() {
    return RecordInitials;
    }

    //method to set surname for record
    public void setSurname(String SurnameString) {
    RecordSurname = SurnameString;
    }

    //method to get surname for record
    public String getSurname() {
    return RecordSurname;
    }

    //method to set mark for record
    public void setMark(int MarkInt) {
    RecordMark = MarkInt;
    }

    //method to get mark for record
    public int getMark() {
    return RecordMark;
    }

    } //end of record class


    public static void main(String[] args) {

    //check that an arg has been supplied
    if (args.length == 0) {
    System.out.println ("no filename specified");
    System.exit(1);
    }

    BufferedReader textFileBuffer = null;

    try {
    File textFile = new File(args[0] + ".txt");
    FileReader textFileReader = new FileReader(textFile);
    textFileBuffer = new BufferedReader(textFileReader);


    String line;
    int z = 0;
    Record[] RecordArray = new Record[100];


    while ((line = textFileBuffer.readLine()) != null) { //while there are still lines to read...
    //reset the 3 variables ready for new values of the next record
    char[] initials = new char[5];
    String surname = " ";
    int mark = 0;

    StringTokenizer token = new StringTokenizer(line);
    int x = 0;
    int y = 0;

    while (token.hasMoreTokens()) { //while there are more tokens on the line...

    String aSingleToken = token.nextToken(); //extract the next token

    //if token is a number it goes into the marks variable
    boolean isNumber = false;
    try {
    mark = Integer.parseInt(aSingleToken);
    isNumber = true;
    }
    catch(NumberFormatException nfe) {
    isNumber = false;
    }
    //if token is a length 1 and not a number it goes into the initials char array
    if (aSingleToken.length() == 1 && !(isNumber)) {
    initials[y] = aSingleToken.charAt(0);
    y++;
    }

    //if token is > 1 and is not a number it goes into String surname variable
    if(aSingleToken.length() > 1 && !(isNumber)) {
    surname = aSingleToken;
    }

    x++;
    } //end of while there are more tokens on the line...

    //the 3 variables above then get put into a Record object and object gets put into an array of record objects



    RecordArray[z] = new Record();
    RecordArray[z].setInitials(initials);
    RecordArray[z].setSurname(surname);
    RecordArray[z].setMark(mark);


    z++;
    } //end of while there are more lines to read



    System.exit(0);


    }
    catch(IOException ioe) {
    System.out.println("Error opening file " +args[0] + ".txt");
    System.exit(1);
    }





    } //end of main method


    //sort array objects into alphabetical order by surname
    //by comparing surname part of each object
    public void RecordSort() {
    int i, j;
    Record temporary;
    for (i = 0; i < RecordArray.length - 1; i++) {
    for (j = RecordArray.length-1 ; j >i; j--) {
    if ((RecordArray[j].getSurname().compareToIgnoreCase( RecordArray[j -1].getSurname()) ) < 0 ) {
    temporary = RecordArray[j];
    RecordArray[j] = RecordArray[j -1];
    RecordArray[j-1] = temporary;
    }
    }
    }

    }




    //method to print out records
    public void PrintRecord() {
    for (int i = 0; i < RecordArray.length - 1; i++) {
    System.out.print(RecordArray[i].getInitials());
    System.out.print(RecordArray[i].getSurname());
    System.out.println(RecordArray[i].getMark());
    }
    }


    } //end of Students class
    Last edited by lisalala; 03-03-2009 at 10:57 AM. Reason: updated code

  5. #5
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default How to use compareTo method

    Argh, actually, scrap that! I have solved my first issue by putting "Record[] RecordArray; right at the top of my code, just below the Results class!
    That's got rid of about 12 error messages all at once! :-)

    The only thing I think I need to sort out now is how to correctly write my compareTo method...

  6. #6
    lisalala is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Update: I have worked out why my comareTo method wasn't working and have corrected that...

    ...I still have one more issue though - I don't know how to call my RecordSort() or my PrintRecord() methods so that the records actually get sorted and printed...

    I have updated my code above to the latest version...any suggestions would be greatly appreciated :-)

Similar Threads

  1. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 PM
  2. Need help. Array sort
    By buzz1500 in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 04:24 AM
  3. Array sort
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-07-2008, 11:41 PM
  4. How to sort an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •