为什么使用Scanner.next()导致这种奇怪的System.out.println()行为?

时间:2015-10-18 01:58:09

标签: java csv java.util.scanner

在尝试final project for my friend's ICS class时,我遇到了这个奇怪的错误。

其中一项任务是阅读this CSV file。这是我使用扫描仪的方式的简短SSCCE:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        Scanner fileScanner;
        String inputFile = "input-project.csv";

        try {
            fileScanner = new Scanner(new File(inputFile));
            fileScanner.useDelimiter("[,\n]");
            fileScanner.nextLine();

            while(fileScanner.hasNext()) {
                String name = fileScanner.next();
                String number = fileScanner.next();
                System.out.println("Inserting " + name + " " + number + " (" + hash(name) + ")");
                //telephoneBook.insert(name, number);
            }

            fileScanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("ERROR: Input file " + inputFile + " was not found");
        }
    }

    public static Integer hash(String name) {
        return name.hashCode() % 7;
    }
}

它读起来很好,但出于某种原因System.out.println("Inserting " + name + " " + number + " (" + hash(name) + ")")只打印" (" + hash(name) + ")"

看看这个输出:

 (5)
 (1)
 (4)
 (5)
 (3)
 (1)
 (4)
 (2)
 (2)
 (2)

Process finished with exit code 0

在尝试解决此问题的代码时,我最终通过不使用fileScanner.next() 来修复它。这是我目前的工作示例:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        Scanner fileScanner;
        String inputFile = "input-project.csv";

        try {
            fileScanner = new Scanner(new File(inputFile));
            //fileScanner.useDelimiter("[,\n]"); 
            fileScanner.nextLine(); // skip the first line

            while(fileScanner.hasNext()) {
                String[] line = fileScanner.nextLine().split(",");
                String name = line[0];
                String number = line[1];
                //String name = fileScanner.next();
                //String number = fileScanner.next();
                System.out.println("Inserting " + name + " " + number + " (" + hash(name) + ")");
                //telephoneBook.insert(name, number);
            }

            fileScanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("ERROR: Input file " + inputFile + " was not found");
        }
    }

    public static Integer hash(String name) {
        return name.hashCode() % 7;
    }
}

按预期打印出来:

Inserting HALL 123-4566 (5)
Inserting BAKER 111-1111 (1)
Inserting CARTER 222-2222 (4)
Inserting KING 555-5555 (5)
Inserting FOX 666-6666 (3)
Inserting LEE 777-7777 (1)
Inserting CASTRO 888-8888 (4)
Inserting DAVIS 999-9999 (2)
Inserting LONG 987-1234 (2)
Inserting KAM 654-7890 (2)

Process finished with exit code 0

我现在的问题是:为什么这会解决问题?我是否错误地使用了.next()方法?

1 个答案:

答案 0 :(得分:2)

您链接的CSV文件具有CRLF行终止符,但您的分隔符仅捕获\n。因此,\r使其成为您的字符串并使输出格式看起来很奇怪。相反,请尝试使用不同的分隔符:

fileScanner.useDelimiter(",|\r?\n");