2017-07-22 81 views
0
/* 
* 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的文件中的記錄進行排序。現在,我收到此錯誤:文件分類 - 掃描儀 - java.util.NoSuchElementException

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

此外,student_records.txt爲空。 :/有人可以幫我嗎?

+0

異常已被修復,但排序後文本文件空出來。 – 123testing123

回答

1

嘗試從改變:

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

到:

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

謝謝。這解決了錯誤,但我的文本文件仍然是空的。 :/ – 123testing123

+0

這行'try(PrintWriter output = new PrintWriter(newfile)){'刪除'student_record.txt'的內容。所以你應該將它移動到'Collections.sort(allRecords,new studentNumberCompare());'行之前。 –

+0

謝謝我已經解決了,但現在我有一個格式錯誤:java.lang.NumberFormatException:對於輸入字符串:「72 \t James」 – 123testing123

0

移動

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

只是輸出迴路之前,這是文件掃描

之前什麼清空和更改掃描從一個FileReader中讀取一個BufferedReader並使用readline,因爲Scaner接下來給出了一個令牌。

+0

對不起,我不知道你的意思。喜歡這個? >>>> Collections.sort(allRecords,new studentNumberCompare()); PrintWriter output = new PrintWriter(newfile); (recordInfo RecordInfo:allRecords){<<<<< 所以我做了上述,但現在我有一個錯誤:線程「主」的異常java.lang.ArrayIndexOutOfBoundsException:1 – 123testing123

+0

好吧,這是下一個錯誤, Scanner.next()爲您提供下一個標記(首先將是什麼學生編號),而不是下一行。 – Turo

+0

謝謝。所以我用這個String記錄= input.nextLine()修復了它;但現在有一個格式錯誤。 :/ java.lang.NumberFormatException:對於輸入字符串:「72 \t James」我沒有將字符串轉換爲int嗎? – 123testing123