文件排序 - 扫描程序 - java.util.NoSuchElementException

时间:2017-07-22 06:11:06

标签: java multithreading exception java.util.scanner

/*
 * 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);

        }

    }

}

基本上,代码是按照升序排列的学号对名为student_records.txt的文件中的记录进行排序。现在,我收到了这个错误:

  

线程“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结果:1

另外,student_records.txt为空。 :/有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

尝试更改:

            String record = input.next();
            while (input.hasNext()){

为:

            while (input.hasNext()){
                String record = input.next();

答案 1 :(得分:0)

移动

 try (PrintWriter output = new PrintWriter(newfile)) {

就在输出循环之前,这就是在扫描之前清空文件的内容

并将Scanner更改为BufferedReader(来自FileReader)并使用readline,因为Scaner接下来会给一个令牌。

相关问题