Results 1 to 11 of 11
Thread: Why is my ArrayList not sorted?
- 04-19-2011, 11:39 PM #1
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
Why is my ArrayList not sorted?
I am trying to use the compareTo method from one class, and sort the data from a file in an ArrayList, but when the program runs, the ArrayList is not sorted.
The data in the file is:
Siena,Catherine,4.0,AppliedMath
Gateau,Jean,3.5,AppliedComputing
Jones,Jill,3.0,AppliedMath
Jones,Jack,2.5AppliedComputing
The names must be sorted alphabetically according to last name, then first name, so that it prints this:
Gateau,Jean,3.5,AppliedComputing
Jones,Jack,2.5AppliedComputing
Jones,Jill,3.0,AppliedMath
Siena,Catherine,4.0,AppliedMath
My compareTo method from the AppliedStudent class:
public int compareTo(Object n)
{
AppliedStudent s = (AppliedStudent)n;
int num = lastname.compareTo(s.getLast());
return (num != 0 ? num : firstname.compareTo(s.getFirst()));
}
public String getLast()
{
return lastname;
}
public String getFirst()
{
return firstname;
}
my main class:
import javax.swing.JFrame;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.*;
public class StudentViewer
{
public static void main(String[] args) throws FileNotFoundException
{
File inputFile;
//File outputFile;
if (args.length >= 1)
{
inputFile = new File(args[0]);
//outputFile = new File(args[1]);
Scanner in = new Scanner(inputFile);
ArrayList<AppliedStudent> names = new ArrayList<AppliedStudent>();
while(in.hasNext() && in.hasNextDouble())
{
String line = in.next();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\s*,\\s*");
String lastname = lineScanner.next();
String firstname = lineScanner.next();
double gpa = lineScanner.nextDouble();
String major = lineScanner.next();
names.add(new AppliedStudent(lastname, firstname, gpa, major));
}
Collections.sort(names);
System.out.println(names);
}
else
throw new FileNotFoundException("File Not Found");
JFrame frame = new StudentFrame(inputFile);
frame.setTitle("Applied MNS Students");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}
- 04-19-2011, 11:43 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Please, as the first thing you do on these forums, wrap your code in
"(code)"
and
"(/code)" (using brackets "[" and "]" instead of using parenthesis) blocks of text, so your code can be read easier. It'll look like this:
Java Code:Your code here.
- 04-19-2011, 11:59 PM #3
1. Your code won't compile
2. That will never be true for your data. It is asking if there is something on the next line and is it a double. Since the first token on each line is a String it will return false.Java Code:while(in.hasNext() && in.hasNextDouble())
-
Have a look at this sample for a neat way of sorting arraylist objects:
Java Code:Person p = new Person("Bruce", "Willis"); Person p1 = new Person("Tom", "Hanks"); Person p2 = new Person("Nicolas","Cage"); Person p3 = new Person("John","Travolta"); ArrayList list = new ArrayList(); list.add(p); list.add(p1); list.add(p2); list.add(p3); Collections.sort(list, new Comparator(){ public int compare(Object o1, Object o2) { Person p1 = (Person) o1; Person p2 = (Person) o2; return p1.getFirstName().compareToIgnoreCase(p2.getFirstName()); } }); System.out.println(list);
Sorting an ArrayList of objects. | Some Java, JEE and WebSphere stuffs
- 04-20-2011, 06:02 AM #5
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
Thanks. My code works just fine on Netbeans at home, but when I get to school and try to run it, it doesn't work, even on Netbeans. The school computers have the latest JDK so I have no clue what the issue is.
- 04-20-2011, 06:08 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Explain it doesn't work.
What errors do you get? Are you providing the files and the right paths on that school computer?
- 04-20-2011, 06:22 AM #7
- 04-20-2011, 05:04 PM #8
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
My program is supposed to read any file the user puts in and sort the data according to last name, then first name.
The file I am testing is in the same directory as my program.
In my AppliedStudent class, my compareTo method is:
Java Code:public int compareTo(Object n) { AppliedStudent s = (AppliedStudent)n; int num = lastname.compareTo(s.getLast()); return (num != 0 ? num : firstname.compareTo(s.getFirst())); }
In my StudentViewer class, when I haveThis occurs: http://i404.photobucket.com/albums/p...z/javapic2.jpgJava Code:while(in.hasNext())
When I includeThis occurs: http://i404.photobucket.com/albums/p...z/javapic1.jpgJava Code:while (in.hasNext() && in.hasNextDouble())
...but the data is not sorted.
-
you never set the Results box in your frame thats why its empty?
EDIT:
so it looks like you got it working with the new while loop but you need to show us the StudentFrame class so we can find out why the results aren't displaying
- 04-20-2011, 06:36 PM #10
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
I did not clarify before that right now, I'm simply trying to get the data sorted and I'm letting the data print out in the console window. The problem is, the data is not sorted.
However, when I process the same code in Netbeans, the ArrayList is sorted.
http://i404.photobucket.com/albums/p...z/javapic3.jpgLast edited by Tabula Rasa; 04-20-2011 at 06:43 PM.
- 04-20-2011, 08:31 PM #11
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Checking if an ArrayList is sorted
By MonkeyGrad in forum New To JavaReplies: 4Last Post: 04-13-2011, 06:27 AM -
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Casting an int from a sorted set
By Bmack in forum New To JavaReplies: 2Last Post: 03-17-2010, 07:09 PM -
Sorted LinkList problem
By koolaqua16 in forum Advanced JavaReplies: 1Last Post: 08-08-2009, 06:49 AM -
My doublyLinked list does not get sorted
By hasani6leap in forum New To JavaReplies: 0Last Post: 01-06-2008, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks