Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Problem with sorting two arrays

    Hey, i have two arrays, studentName and studentMark
    I need to sort the array by name, and by mark, although obviously the student needs to keep their mark in the same array location, if you get me..

    here is what I've got so far:

    Java Code:
    package assignment1;
    
    public class UnitResults2 
    {
        private String [] studentName = new String [20];
        private int [] studentMark  = new int [20];
        public String unitTitle;
        private static String course;
        private static int pointer;
        
    public UnitResults2()
        {
            pointer = 0;
        }
    public String displayAll()
        {
            String strOutput = "";
            //System.out.println("Students\n");
            strOutput = strOutput + "Student List for the " + unitTitle + " Unit\n";
            
            if(pointer == 0)
                {    
                    //System.out.println("There are no Elemets in the Array\n");
                    strOutput = strOutput + "There are no Students in the Class\n";
                    return strOutput;
                }
            
            for(int index = 0; index < pointer; index++)
                {
                    //System.out.println(studentName[index] + " " + studentMark[index]);
                    strOutput = strOutput + studentName[index] + " " + studentMark[index] + "\n";
                }
            return strOutput;
        }//End of display Method
        
    public boolean add(String foreName, String surName, int mark)
        {
            //String strOutput = "";
            if(pointer == studentName.length)
            {
                System.out.println("The Class is full!\n");
                //strOutput = strOutput + "The Class is full!\n";
                return false;
            }
            
            studentName[pointer] = (foreName + " " + surName);
            studentMark[pointer] = mark;
            pointer++;
            
            //strOutput = strOutput + "Student was successfully added.";
            
            return true;
        }//End of add Method.
        
    public boolean delete(int element)
        {
            element = element - 1; //array location
            if ((element >= 0) && (element < pointer))
            {
                for(int index = (element + 1); index < pointer; index ++)
                {
                    studentName[index -1] = studentName[index];
                    studentMark[index -1] = studentMark[index];
                }
                pointer--;
                return true;
            }
            else
            {
                return false;
            }
        }//End of delete Method
    
    public String searchStudentNumber (int element)
        {
            String strOutput;
            int tempPointer;
      
            element = element - 1;
            tempPointer = pointer;
            pointer = element;
            
            if (pointer < 0)
            {
                strOutput = "There are no Elements in the Array\n";
                pointer = tempPointer;
                return strOutput;
            }
            else if (pointer > studentName.length)
            {
                strOutput = "This area in space is empty.\n";
                pointer = tempPointer;
                return strOutput;
            }
            else
            {
                
                strOutput = "The student with the ID: " + element + " is:\n" + studentName[pointer] + " " + studentMark[pointer] + "\n";
                pointer = tempPointer;
                return strOutput;
            }   
        }//end of searchStudentNumber
    
    public int updateResult(int element, int NewResult)
    {
            int tempPointer;
            element = element - 1;
            tempPointer = pointer;
            pointer = element;
            
            studentMark[pointer] = NewResult;
            pointer = tempPointer;
            return studentMark[pointer];
    }end of updateResult method
    
    public String updateName(int element, String NewForename, String NewSurname)
    {
            int tempPointer;
            element = element - 1;
            tempPointer = pointer;
            pointer = element;
            
            studentName[pointer] = NewForename + " " + NewSurname;
            pointer = tempPointer;
            return studentName[pointer];
    }end of updateName method
    
    public void sort()
    {
        boolean swapped;
        do{
            swapped = false;
            for (int i = 0; i < studentName.length-1; i++) {
                if(studentName[i].compareTo(studentName[i+1])>0){
                    String tempName = studentName[i+1];
                    studentName[i+1] = studentName[i];
                    studentName[i] = tempName;
                     
                    int tempResult = studentMark[i+1];
                    studentMark[i+1] = studentMark[i];
                    studentMark[i] = tempResult;
                         
                    swapped=true;
                }
            }
        }while(swapped);            
    }//end of sort method
    }//end of class
    when i run my testClass, i get this error when trying to use the sort method:

    at java.lang.String.compareTo(String.java:1139)
    at assignment1.UnitResults2.sort(UnitResults2.java:13 4)
    at assignment1.TestUnitResults.main(TestUnitResults.j ava:44)

    any help with this would be much appreciated!

    Thanks, Jason.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    I think you need to post the full error, not just a bit of the stack trace.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    This is my test class:
    Java Code:
    package assignment1;
    
    public class TestUnitResults 
    {
        public static void main(String[] args) 
        {
            UnitResults2 Web = new UnitResults2();           //creating an object of UnitResults2
            UnitResults2 DSA = new UnitResults2(); 
            
            
            Web.unitTitle = "Web Development";              //Assigning the unitTitle a value
            DSA.unitTitle = "Data Structure Algorithms";
            
            
            
            Web.add("Bob", "Simpson", 78);                   //adding student details
            Web.add("Jason", "McClenaghan", 100);
            Web.add("Jimmy", "Rascal", 68);                  
            Web.add("Paul", "Massey", 43);
            
            
            
            System.out.println(Web.displayAll());            //displaying all student details
            
            
            
            Web.updateResult(1, 33);                         //updating student result
            System.out.println(Web.displayAll());            //displaying all student details
            
            
            
            Web.updateName(2, "John", "Mcclean");            //updating student name
            System.out.println(Web.displayAll());            //displaying all student details
            
            
            
            System.out.println(Web.searchStudentNumber(2));  //search  for student details using "Student Number"
            
            
            
            Web.delete(2);                                   //delete student from class list
            System.out.println(Web.displayAll());            //displaying all student details
            
            Web.sort();
            System.out.println(Web.displayAll());
            
        }
    }
    When run this, i get this output:

    Java Code:
    run:
    Student List for the Web Development Unit
    ------------------------------------------
    Bob Simpson 78
    Jason McClenaghan 100
    Jimmy Rascal 68
    Paul Massey 43
    
    Student List for the Web Development Unit
    ------------------------------------------
    Bob Simpson 33
    Jason McClenaghan 100
    Exception in thread "main" java.lang.NullPointerException
    Jimmy Rascal 68
    Paul Massey 43
    
    Student List for the Web Development Unit
    ------------------------------------------
    Bob Simpson 33
    John Mcclean 100
    Jimmy Rascal 68
    Paul Massey 43
    
    The student with the ID: 1 is:
    John Mcclean 100
    
    Student List for the Web Development Unit
    ------------------------------------------
    Bob Simpson 33
    Jimmy Rascal 68
    Paul Massey 43
    
    	at java.lang.String.compareTo(String.java:1139)
    	at assignment1.UnitResults2.sort(UnitResults2.java:134)
    	at assignment1.TestUnitResults.main(TestUnitResults.java:44)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    Your output and error streams are interleaved. This is the error trace:

    Quote Originally Posted by jason3460 View Post
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.lang.String.compareTo(String.java:1139)
    	at assignment1.UnitResults2.sort(UnitResults2.java:134)
    	at assignment1.TestUnitResults.main(TestUnitResults.java:44)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    So, what's on UnitResults2.java at line 134?

    Also note that according to the code conventions for the Java programming language, variable names should start with a lowercase letter. Your code is confusing on account of not following the conventions.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    Okay thanks,
    this is the code on line 134(UnitResults2.java is posted above):
    Java Code:
                if(studentName[i].compareTo(studentName[i+1])>0){
    I believe my variables all start with a lowercase letter? Could you point out which areas are confusing ?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    Quote Originally Posted by jason3460 View Post
    I believe my variables all start with a lowercase letter?
    Really?
    Quote Originally Posted by jason3460 View Post
    Java Code:
            UnitResults2 Web = new UnitResults2();           //creating an object of UnitResults2
            UnitResults2 DSA = new UnitResults2();
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    Quote Originally Posted by jason3460 View Post
    Okay thanks,
    this is the code on line 134(UnitResults2.java is posted above):
    Java Code:
       if(studentName[i].compareTo(studentName[i+1])>0){
    So figure out which of studentName[i] or studentName[i + 1] is null, and why. Adding a couple of sysouts to see the values before that method call should help to identify the cause of the NPE.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    I'll throw a bone here, because the stack trace shows that the NPE is thrown from compareTo, so studentName[i] cannot be null.
    It must be studentName[i+1].

    But you'll still need to stick some debug sysouts in there to find out exactly which one is causing the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    Quote Originally Posted by DarrylBurke View Post
    Really?


    db
    would that technically not be an object ? and not a variable... I'm not sure since I'm 'New to Java' sorry.

    Quote Originally Posted by Tolls View Post
    I'll throw a bone here, because the stack trace shows that the NPE is thrown from compareTo, so studentName[i] cannot be null.
    It must be studentName[i+1].

    But you'll still need to stick some debug sysouts in there to find out exactly which one is causing the problem.
    I'm not quite sure what you mean by debug sysouts ? could you maybe explain ?

    Thanks Jason.

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    Java Code:
    System.out.println("something of use in debugging here");
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    Alright so i attempted to add a debug sout, and i get 'Paul Massey' outputted 3 times before the error appears, so when the sort method is called, the first three areas in the array contain:
    [0] - Bob Simpson
    [1] - Jason McClenaghan
    [2] - Paul Massey

    So what does this mean ? the loop is getting stuck ? I'm not too sure...?

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    Well, it depends where you've put your debugging code.
    You might want to look at putting more than one in, with things like the index maybe.
    Anything that might point ot the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    Not getting anything from this so far ! Is there another method of sorting i could use? I cant seem to get this to work at all!

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    What does your sorting code look like now (complete with debug statements) and what does it print out?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with sorting two arrays

    Maybe you want to read one of my blogs on this forum (it discusses your problem).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    Java Code:
    public void sort()
    {
        boolean swapped;
        do
        {
            swapped = false;
            for (int i = 0; i < studentName.length-1; i++) 
            {
                System.out.println("Debug 1: " + studentName[i]);
                
                if(studentName[i].compareTo(studentName[i+1])>0)
                {
                    //System.out.println("Debug 2: " + studentName[i]);
                    
                    String tempName = studentName[i+1];
                    studentName[i+1] = studentName[i];
                    studentName[i] = tempName;
    
                    //System.out.println("Debug 3: " + studentName[i]);
                    
                    int tempResult = studentMark[i+1];
                    studentMark[i+1] = studentMark[i];
                    studentMark[i] = tempResult;
    
                    //System.out.println("Debug 4: " + studentName[i]);
                    
                    swapped=true;
                }
            }
        }
        while(swapped);            
    }//end of sort Method
    I've tried the debug in them 4 places, debug1 outputs:
    Debug 1: Bob Simpson
    Debug 1: Jimmy Rascal
    Debug 1: Paul Massey
    Debug 1: Paul Massey

    Although debugs 2,3 and 4, do not output anything.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with sorting two arrays

    Quote Originally Posted by jason3460 View Post
    Although debugs 2,3 and 4, do not output anything.
    Of course not because those statements are commented out and even if they weren't they wouldn't be executed because those names are already in lexicographic order.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    Quote Originally Posted by jason3460 View Post
    [code]
    I've tried the debug in them 4 places, debug1 outputs:
    Debug 1: Bob Simpson
    Debug 1: Jimmy Rascal
    Debug 1: Paul Massey
    Debug 1: Paul Massey

    Although debugs 2,3 and 4, do not output anything.
    Well, it won't, as they are already in order, so the compareTo is never > 0.
    You do appear to have more than one Paul Massey in there, though.

    As for your null pointer do you ensure you have filled all 20 slots in the array before sorting?
    If not then you could break out of the loop when studentname[i+1] is null, as that would be then end of your array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Problem with sorting two arrays

    Quote Originally Posted by JosAH View Post
    Of course not because those statements are commented out and even if they weren't they wouldn't be executed because those names are already in lexicographic order.

    kind regards,

    Jos
    Well i had them uncommented before hand, although i wasn't aware they wouldn't work anyway, thanks.

    Quote Originally Posted by Tolls View Post
    Well, it won't, as they are already in order, so the compareTo is never > 0.
    You do appear to have more than one Paul Massey in there, though.

    As for your null pointer do you ensure you have filled all 20 slots in the array before sorting?
    If not then you could break out of the loop when studentname[i+1] is null, as that would be then end of your array.
    Alright, I hadn't got all 20 slots filled, now i have them filled, and the sorting it working.. sorry about that, I didn't realize that would disturb the sort..
    although in my program i have updateName and updateMark methods, which seem to disturb the pointer location(i think) this is the error i get when I execute the method updateResult:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    at assignment1.UnitResults.updateResult(UnitResults.j ava:117) = return studentMark[pointer];
    at assignment1.TestUnitResults.main(TestUnitResults.j ava:43) = Web.updateResult(1, 33);
    Java Result: 1

    its the same for the updateName although studentName[pointer] and updateName("", "");

    you can see my full methods above if you wish, thanks Jason.

  20. #20
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Problem with sorting two arrays

    At some point you are attempting to access studentMark[20].
    So 'pointer' is 20...
    19 is the maximum index.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. Sorting arrays
    By Blondedude092 in forum New To Java
    Replies: 2
    Last Post: 12-01-2012, 01:57 AM
  2. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 02:19 AM
  3. Help with sorting arrays
    By Joycey in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 08:35 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 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
  •