Java输出列表结构不正确

时间:2016-12-14 00:22:08

标签: java java.util.scanner

我有一个文本文件,其中包含以下内容(部分):

attributes_Accepts_Credit_Cards,
attributes_Accepts_Insurance,
attributes_Ages_Allowed,
attributes_Alcohol,
attributes_Ambience_casual,
attributes_Ambience_classy,
attributes_Ambience_divey,
attributes_Ambience_hipster,
attributes_Ambience_intimate,
attributes_Ambience_romantic,
attributes_Ambience_touristy,
attributes_Ambience_trendy,
attributes_Ambience_upscale,

我正在尝试获得一个输出,这样我就会得到一个包含以下附加字符的列表:

business['attributes_Accepts_Credit_Cards'],
business['attributes_Accepts_Insurance'],
business['attributes_Ages_Allowed'],

但是目前我的代码以下列方式输出:

business['attributes_Accepts_Credit_Cards'],
business['
attributes_Accepts_Insurance'],
business['
attributes_Ages_Allowed'],
business['
attributes_Alcohol'],
business['
attributes_Ambience_casual'],
business['
attributes_Ambience_classy'],
business['
attributes_Ambience_divey'],
business['
attributes_Ambience_hipster'],
business['
attributes_Ambience_intimate'],

代码:

public class TestScanner {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner scanner = new Scanner(new File("/list.txt"));
        scanner.useDelimiter(",");
        while(scanner.hasNextLine()){
            System.out.println("business['"+ scanner.next()+"'],");
        }
        scanner.close();
    }

}

1 个答案:

答案 0 :(得分:1)

您需要将换行符添加到分隔符:

scanner.useDelimiter(",\n");