File Sorting
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package filesort;
import java.io.File;
import java.util.Scanner;`enter code here`
import java.io.IOException;
import java.util.Comparator;
import java.util.Collections;
import java.util.ArrayList;
import java.io.PrintWriter;
/**
*/
/**
* @param args the command line arguments
*/
class recordInfo{
int studentNumber;
String firstName;
public recordInfo(int studentNumber,String firstName){
this.studentNumber = studentNumber;
this.firstName = firstName;
}
}
class firstNameCompare implements Comparator<recordInfo>{
@Override
public int compare(recordInfo t, recordInfo t1) {
return t.firstName.compareTo(t1.firstName);
}
}
class studentNumberCompare implements Comparator<recordInfo>{
@Override
public int compare(recordInfo t, recordInfo t1) {
return t.studentNumber - t1.studentNumber;
}
}
public class FileSort {
public static void main(String[] args) throws IOException{
File newfile = new File("student_record.txt");
// try{
// PrintWriter output = new PrintWriter(newfile);
// output.println("72" + "t" +"James");
// output.println("56" + "t" +"Mark");
// output.println("87" + "t" +"John");
// output.println("30" + "t" +"Phillip");
// output.println("44" + "t" +"Andrew");
// output.close();
// }
//
// catch(IOException ex){
// System.out.printf("error n", ex);
//
// }
try{
Scanner input = new Scanner(newfile);
try (PrintWriter output = new PrintWriter(newfile)) {
ArrayList<recordInfo> allRecords = new ArrayList<>();
String record = input.next();
while (input.hasNext()){
String[] oneRecord = record.split(" ");
int studentNumber = Integer.valueOf(oneRecord[0]);
String firstName = oneRecord[1];
allRecords.add(new recordInfo(studentNumber, firstName));
//System.out.printf("%s %s n", studentNumber, firstName);
}
Collections.sort(allRecords, new studentNumberCompare());
for (recordInfo RecordInfo : allRecords){
output.println(RecordInfo.firstName);
output.println("t" +RecordInfo.studentNumber);
output.println("n");
System.out.println(" " + RecordInfo.studentNumber + " " + RecordInfo.firstName);
}
output.close();
}
}
catch(IOException ex){
System.out.printf("error n", ex);
}
}
}
Basically the code is to sort records in the file called student_records.txt by student numbers arranged in ascending order. Right now, I'm getting this error:
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at filesort.FileSort.main(FileSort.java:79) Java Result: 1
ALSO, student_records.txt is empty. :/ Anyone available to help me please?
Try changing from:
String record = input.next();
while (input.hasNext()){
to:
while (input.hasNext()){
String record = input.next();
Move
try (PrintWriter output = new PrintWriter(newfile)) {
just before the output loop, that's what empties the file before scanning
and change the Scanner to a BufferedReader(from a FileReader) and use readline, since Scaner next gives onla a token.
链接地址: http://www.djcxy.com/p/96084.html上一篇: 从txt文件读取ints java错误
下一篇: 文件分类