2013-01-20 53 views
1

我正在使用掃描器方法嘗試獲取文件輸入。問題是我只在First name和Last name之後得到第一個int。這裏是文本文件:解析文件輸入中的數字

John Doe 12 6 8 15 35 16 17 4 12 54 
Mark Doe 16 67 8 12 35 19 78 3 12 101 
Johnnie Smarts 20 10 10 20 40 100 20 10 20 100 
Frank Noshow 
Molly Guess 2 5 6 7 2 4 7 8 9 10 

我有這麼多的代碼,這樣做的遠:

import java.io.*; 
import java.util.*; 
import static java.lang.Math.*; 
import static java.lang.System.out; 

public class StudentTester 
{ 
    public static void main (String[] args) throws IOException 
    { 
     Scanner kb = new Scanner(System.in); 
     System.out.println("\tWelcome.. "); 
     System.out.println(" Please enter the file name. "); 
     String filename = kb.nextLine(); 
     Scanner fileReader = new Scanner(new File(filename)); 
     //created a scanner object with a file object. 
     //Creat a String array to hold each lince of the file. 
     String [] s = new String [10000]; //prepared for a 100 lines of text. 
     int linesOfText = -1; 
     int score[] = new int[10]; 
     while (fileReader.hasNextLine())//while the file has any lines. 
     { 
      linesOfText++; 

      s[linesOfText] = fileReader.nextLine(); 
      //This places the first line of the file in the s array. 
      //System.out.println(s[linesOfText]);//for testing purposes.. 
     } 
     fileReader.close();//close the Scanner. 

     for(int i=0;i<=linesOfText;i++){ 
      Scanner sc = new Scanner(s[i]); 
      String fN = sc.next(); 
      String lN = sc.next(); 

      if(sc.hasNext()){ 
       score[i] = sc.nextInt();     
      } 
      else { 
       System.out.println(" opps, there is no score here... "); 
      } 
      Student stu = new Student(fN,lN,score); 
      System.out.println(score[i]); 
     } 
    } 
} 
+2

你如何期待* *它來讀取一個值嗎?你只有一次'nextInt',並根據你正在記錄的記錄填充'score [i]',而不是某個特定的學生。我建議提取代碼,將單個學生的數據讀入單獨的方法 - 這會使它簡單得多。 –

+0

Thxx用於編輯! –

回答

0

那是因爲你做了next()後跟一個next()然後nextInt()

只需更換

if (sc.hasNext()) {...} 

while (sc.hasNext()) {...} 
+0

我用while替換了它,但它仍然不起作用,它打印出第一個數字,然後進入下一行。 –

0
I think the following code can help you for your request : 

package com.mongolhun.tech.basic.miscellaneous; 
import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 

public class StudentTester 
{ 
    public static void main (String[] args) throws IOException 
    { 
     Scanner kb = new Scanner(System.in); 
     System.out.println("/tWelcome.. "); 
     System.out.println(" Please enter the file name. "); 
     String filename = kb.nextLine(); 
     Scanner fileReader = new Scanner(new File("D:/workspace/test/src/test/java/com/yxyy/tech/basic/miscellaneous/" +filename)); 
     //created a scanner object with a file object. 
     //Creat a String array to hold each lince of the file. 
     String [] s = new String [10000]; //prepared for a 100 lines of text. 
     int linesOfText = -1; 
     int score[] = new int[10]; 
     while (fileReader.hasNextLine())//while the file has any lines. 
     { 
      linesOfText++; 

      s[linesOfText] = fileReader.nextLine(); 
      //This places the first line of the file in the s array. 
      //System.out.println(s[linesOfText]);//for testing purposes.. 
     } 
     fileReader.close();//close the Scanner. 

     for(int i=0;i<=linesOfText;i++){ 
      Scanner sc = new Scanner(s[i]); 
      String fN = sc.next(); 
      String lN = sc.next(); 
      int a = 0; 
      while(sc.hasNext()){ 
       score[i] = sc.nextInt();  
       System.out.print(score[i] + " "); 
       a = 1; 
      } 

      if (a == 0) { 
       System.out.print("opps, there is no score here... "); 
      } 
      System.out.println(); 
//   Student stu = new Student(fN,lN,score); 

     } 
    } 
} 
+0

謝謝!這是非常有用的... –