为什么我的程序不能正确识别姓?

问题描述:

扫描器读取错误的数据,文本文件格式是:为什么我的程序不能正确识别姓?

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

它应该打印史密斯和琼斯 –

首先请换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(“,”),如何解决它? –

问题是,您的数据不只是用逗号分隔。它也由行结束,也由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不是默认编码,所以我需要指定它。