简体   繁体   English

使用扫描程序和JAVA中的多个分隔符从文本文件中读取数据

[英]Read data from text file with scanner and multiple delimiters in JAVA

I have a text file containg the following data: 我有一个包含以下数据的文本文件:

20;
1: 39, 63;
2: 33, 7;
16: 33, 7;
3: 45, 27;
4: 8, 67;
5: 19, 47;
6: 15, 40;
...
20: 65, 54;

first integer is the amount of entries in the list. 第一个整数是列表中的条目数量。

Each line is an entry. 每行都是一个条目。 So entry 1 has xy coordinates 39 and 63 respectively. 因此,条目1分别具有xy坐标39和63。 I understand that I can use different delimiters to read the data, but i'm not quite sure how to achieve that. 我知道我可以使用不同的分隔符来读取数据,但我不太清楚如何实现这一点。 Currently I'm using split() . 目前我正在使用split()

The following code reads each line, but this obviously doesn't work since the delimiters are not set propperly. 以下代码读取每一行,但这显然不起作用,因为分隔符未正确设置。 Is there a good way to get this to work? 是否有一个很好的方法让这个工作?

String[] temp = sc.next().split(";");
int productAmount = Integer.parseInt(temp[0]);
sc.nextLine();

for (int i = 0; i < productAmount; i++) {
    int productID = sc.nextInt();
    int x = sc.nextInt();
    int y = sc.nextInt();
    Product product = new Product(x, y, productID);
    productList.add(product);
}

All of the tokens can be converted to integers after removing the last character from them. 从中移除最后一个字符后,可以将所有标记转换为整数。 You can make use of this property of the given data. 您可以使用给定数据的此属性。 Define a method like this: 定义这样的方法:

int integerFromToken(String str) {
    return Integer.valueOf(str.substring(0, str.length() - 1));
}

which returns the integer part of a token ( 39, returns 39 , 63; returns 63 etc.). 它返回一个令牌的整数部分( 39,返回3963;返回63等)。 Now use the method to get integer values from tokens (obtained with sc.next() ): 现在使用该方法从标记获取整数值(使用sc.next()获得):

int productID = integerFromToken(sc.next());
int x = integerFromToken(sc.next());
int y = integerFromToken(sc.next());

If you use 如果你使用

String[] temp = sc.next().split("[ ,:;]");

then your temp variable will hold only the numbers. 那么你的temp变量只会包含数字。 The string "[ ,:;]" is a regular expression , and it means any character in the square brackets will be a delimiter. 字符串"[ ,:;]"是一个正则表达式 ,它表示方括号中的任何字符都是分隔符。

Very similar to what Croo said, but I get a compilation error when I use his method. 非常类似于Croo所说的,但是当我使用他的方法时,我得到了编译错误。

In order to avoid matching empty strings by including spaces as delimiters, it makes more sense to me to allow them and use a call to trim to get rid of them. 为了避免通过将空格包含为分隔符来匹配空字符串,我允许它们并使用trim来删除它们更有意义。

Also it be useful to know that the Scanner can be given a regex expression as a delimiter and then calls to next can be used to navigate the input. 同样有用的是知道可以将Scanner作为分隔符给出regex表达式,然后可以使用对next调用来导航输入。

sc.useDelimiter("[,:;]");
int productAmount = Integer.parseInt(sc.next());
int x, y, productID, i;
for (i = 0; i < productAmount; i++) {
    productID = Integer.parseInt(sc.next().trim());
    x = Integer.parseInt(sc.next().trim());
    y = Integer.parseInt(sc.next().trim());
    productList.add(new Product(x,y,productID));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM