2016-02-10 60 views
0

我正在製作一個程序,從輸入文件中獲取學生記錄並將它們存儲在數組列表中。我抓住學生證,姓名和三個考試成績。每個項目由製表符分隔。我需要幫助把學生的名字拼湊起來。如果我像這樣運行,它會混亂,因爲有些名字有姓氏,名字和中間名。爲了詳細說明我的主要問題,我試圖抓住I.E.的名字。 J Singleton不在文件中並進入我的數組列表。我只是抓住了名字,然後把姓氏放在一起,但它並不總是有效,因爲它們中的一些有三個不同的東西可以抓取,例如上面的示例。由於在文件名稱前後有一個選項卡,有沒有辦法抓住整個名稱?我需要幫助從輸入文件中拉空間的字符串

公共類主要{

public static void main(String[] args) throws Exception { 
    String stdID; 
    int tScore1; 
    int tScore2; 
    int tScore3; 
    String sName; 
    String fName; 
    String lName; 
    Students workobj; 
    try{ 
     //opening the file for input 
     FileInputStream istream = new FileInputStream("input.txt"); 
     Scanner input = new Scanner(istream); 
     //creating an arraylist to store student objects 
     ArrayList<Students> AllStudents= new ArrayList<Students>(); 

     while(input.hasNextLine()){ 

      //first I will read the student id 
      stdID=input.next(); 
      //remove later 
      System.out.println("stdEcho "+ stdID); 
      //next I will read the student name 
      fName= input.next(); 
      lName=input.next(); 

      sName=(fName+lName); 
      //remove later 
      System.out.println("NameEcho " + sName); 
      //next read in the test scores 
      tScore1=input.nextInt(); 
      //remove later 
      System.out.println("Test01Echo " +tScore1); 
      tScore2=input.nextInt(); 
      //remove later 
      System.out.println("Test02Echo " +tScore2); 
      tScore3=input.nextInt(); 
      //remove later 
      System.out.println("Test03Echo " +tScore3); 



      //printing the record 
      System.out.println("Student ID: "+stdID + " Student Name: " + sName + " Test Score 1: " +tScore1 
      + " Test Score 2: " + tScore2 + " Test Score 3: " + tScore3); 
      output.println("Student ID: "+stdID + " Student Name: " + sName + " Test Score 1: " +tScore1 
        + " Test Score 2: " + tScore2 + " Test Score 3: " + tScore3); 
      //creating a student object 
      Students StudentRecord= new Students(stdID,sName,tScore1,tScore2,tScore3); 
      StudentRecord.listStudents(); 
      //now store this in allstudents 
      AllStudents.add(StudentRecord); 
     }//end of while 
     //Now I will list the records 
     System.out.println("Getting Students from AllStudents Container"); 
     for(int i=0;i<=AllStudents.size()-1;i++){ 
      //retrieving the object 
      workobj=AllStudents.get(i); 
      workobj.listStudents(); 
     }//end of for 
     System.out.println("This is the sorted values of Students from the AllStudents container"); 
     sortLarge(AllStudents); 
     for(int i=0; i<=AllStudents.size()-1;i++){ 
      workobj=AllStudents.get(i); 
      workobj.listStudents(); 
     } 

    }//end of try 

     catch (FileNotFoundException e){ 
      System.out.println("file not found"); 
      System.err.println("File not found"); 
      System.exit(11); 
    }// end catch 
      catch (InputMismatchException e){ 
       System.out.println("Error in Reading File"); 
       System.err.println("Error in Reading File"); 
       System.exit(10); 
      } 
    finally { 
     output.close(); 
     System.exit(2); 
    } 
} 
+0

你能詳細說明你確切的問題嗎? – ramasCoder

+0

我編輯它,所以希望它現在更清楚一點。 – benderbot2004

回答

0

使用String類分割功能等 S.split( 「\ t」 的); 這將從源頭標籤 您可以瞭解的基礎上,分離在 https://docs.oracle.com/javase/7/docs/api/java/lang/String.html 返回字符串數組,這裏是你可以嘗試的代碼,一個函數來獲取從文件 文件名學生記錄作爲參數傳遞。確保你有id,姓名和每個分數之間的選項卡。

// Create list of All Records 
    public ArrayList<Students> getStudents(String fileName) throws Exception 
    { 
     System.out.println("2: Please Wait .... "); 
     String stdID; 
     int tScore1; 
     int tScore2; 
     int tScore3; 
     String sName; 
     //reading from "input.txt" 
     File data=new File(fileName); 
     FileReader reader=new FileReader(data.getAbsoluteFile()); 
     BufferedReader breader= new BufferedReader(reader); 

     String eachrow; 
     ArrayList<Students> AllStudents= new ArrayList<Students>(); 
     while((eachrow = breader.readLine()) != null) 
     { 
      String []r = eachrow.split("\t") 
       stdID = r[0]; 
       sName = r[1]; 
       tScore1 = r[2]; 
       tScore2 = r[3]; 
       tScore3 = r[4]; 
       //creating a student object 
       Students StudentRecord= new Students(stdID,sName,tScore1,tScore2,tScore3); 
       StudentRecord.listStudents(); 
       //now store this in allstudents 
       AllStudents.add(StudentRecord); 
     } 

     System.out.println("Student Records are Created Successfully."); 
     System.out.println("-----------------------------------------------------------------"); 
     return AllStudents; 
    } 
+0

你能告訴我如何實現這個嗎?我正在嘗試,並不斷收到錯誤。 – benderbot2004

+0

我已經更新了我的答案檢查現在並刪除任何錯誤,如果您自己發生。 –