2015-09-06 57 views
0

掃描器讀取錯誤的數據,文本文件格式是:爲什麼我的程序不能正確識別姓?

111,Smith,Sam, 40,10.50 
330,Jones,Jennifer,30,10.00

的程序是:

public class P3 { 
    public static void main(String[] args) { 
     String file=args[0]; 
     File fileName = new File(file); 
     try { 
      Scanner sc = new Scanner(fileName).useDelimiter(", "); 
      while (sc.hasNextLine()) { 
       if (sc.hasNextInt()){ int id = sc.nextInt();} 
       String lastName = sc.next(); 
       String firstName = sc.next(); 
       if (sc.hasNextInt()){ int hours = sc.nextInt(); } 
       if (sc.hasNextFloat()){ float payRate=sc.nextFloat(); } 
       System.out.println(firstName); 
      } 
      sc.close(); 
     } catch(FileNotFoundException e) { 
      System.out.println("Can't open file "  
           + fileName + " "); 
     } 
    } 
} 

輸出是:

40,10.50 
330,Jones,Jennifer,30,10.00 

它應該是:

Sam 
Jennifer 

我該如何解決?

+0

那麼史密斯和瓊斯呢? –

+0

if is System.out.println(lastName); –

+0

它應該打印史密斯和瓊斯 –

回答

0

首先請換fileNamefile。接下來,我建議你使用try-with-resources。如果你打算使用它們,你的變量需要處於一個共同的範圍。最後,在可選空格和逗號使用時hasNextLine()然後,我會呼叫nextLine,你可以split。這可能看起來像

String fileName = // ... 
File file = new File(fileName); 
try (Scanner sc = new Scanner(file)) { 
    while (sc.hasNextLine()) { 
     String line = sc.nextLine(); 
     String[] arr = line.split("\\s*,\\s*"); 
     int id = Integer.parseInt(arr[0]); 
     String lastName = arr[1]; 
     String firstName = arr[2]; 
     int hours = Integer.parseInt(arr[3]); 
     float payRate = Float.parseFloat(arr[4]); 
     System.out.println(firstName); 
    } 
} catch (FileNotFoundException e) { 
    System.out.println("Can't open file " + fileName + " "); 
    e.printStackTrace(); 
} 
+0

如果我們必須使用掃描儀(文件).useDelimiter(「,」),如何解決它? –

3

問題是,您的數據不只是用逗號分隔。它也由行結束,也由Unicode character U+FF0C(FULLWIDTH COMMA)分隔。

我把你的代碼,取而代之的是線

Scanner sc = new Scanner(fileName).useDelimiter(", "); 

Scanner sc = new Scanner(fileName, "UTF-8").useDelimiter(", |\r\n|\n|\uff0c"); 

,然後運行它。它產生了它應有的輸出。

文本, |\r\n|\n|\uff0c是一個正則表達式匹配可以:

  • 逗號後跟一個空格,
  • 跟着一個新行(\n)回車(\r),
  • 一個換行符本身,
  • Unicode全角逗號(\uff0c)。

這些是我們想要分隔文本的字符。我已經指定了兩種類型的換行符,因爲我不確定文件使用哪個行結束符。

我還設置掃描儀在從文件讀取時使用UTF-8編碼。我不知道這是否會對你有所幫助,但在我的系統上,UTF-8不是默認編碼,所以我需要指定它。

相關問題