Results 1 to 8 of 8
- 04-17-2011, 02:37 AM #1
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
-
You might consider telling us more detail.
- 04-17-2011, 02:57 AM #3
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
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]);
AppliedStudent nameArray[] = inputFile; (This is wrong but I don't know how to correct it.)
List<AppliedStudent> names = Arrays.asList(nameArray);
Collections.sort(names);
System.out.println(names);
JFrame frame = new StudentFrame(inputFile);
frame.setTitle("Students");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
else
throw new FileNotFoundException("File Not Found");
}
}
-
why not just iterate through the file in a while loop, possibly using a Scanner object?
- 04-17-2011, 03:28 AM #5
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
Scanner in = new Scanner(new File(args[0]));
for(int i=0; in.hasNext(); i++)
while (in.hasNext())
{
?
}
-
You don't want both a for loop and a while loop. A while loop alone is probably best, and you'll likely want to check hasNextLine and then in the loop, get nextLine() depending on the text file's format.
- 04-17-2011, 03:53 AM #7
Member
- Join Date
- Apr 2011
- Location
- USA
- Posts
- 15
- Rep Power
- 0
It reads and prints out the data.
I have to alphabetize the data, so how do I apply the compareTo method I did in a different class?
public int compareTo(Object n)
{
AppliedStudent s = (AppliedStudent)n;
int num = lastname.compareTo(lastname);
return (num != 0 ? num : firstname.compareTo(firstname));
}
I tried to use Collections.sort but it didn't apply the method.
The names are supposed to be sorted by last name, then first name.
-
compareTo is used in a class that implements the Comparable interface. So if AppliedStudent or its parent implements this interface, then you're all set. If not, then you can't use Comparator or its compareTo method, but will have to create a Comparator<AppliedStudent> utility class and this class will contain one static method called compare that will take two AppliedStudent objects as parameters).
Similar Threads
-
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
Read txt file into arraylist
By nickerhardt in forum New To JavaReplies: 9Last Post: 08-04-2010, 04:34 PM -
Read txt file to arrayList
By koddy in forum New To JavaReplies: 14Last Post: 04-29-2010, 05:15 PM -
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
PROOF READ: Sort text file 3 different ways and compare
By VinceGuad in forum New To JavaReplies: 2Last Post: 01-26-2009, 04:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks