我想从一个文本文件中读取,并将其分为三个不同的类别。 ID,地址,和重量。但是,每当我尝试访问的地址和重量我有一个错误。有没有人看到这个问题?
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
class Project1
{
public static void main(String[] args)throws Exception
{
List<String> list = new ArrayList<String>();
List<String> packages = new ArrayList<String>();
List<String> addresses = new ArrayList<String>();
List<String> weights = new ArrayList<String>();
//Provide the file path
File file = new File(args[0]);
//Reads the file
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while((str = br.readLine()) != null)
{
if(str.trim().length() > 0)
{
//System.out.println(str);
//Splits the string by commas and trims whitespace
String[] result = str.trim().split("\\s*,\\s*", 3);
packages.add(result[0]);
//ERROR: Doesn't know what result[1] or result[2] is.
//addresses.add(result[1]);
//weights.add(result[2]);
System.out.println(result[0]);
//System.out.println(result[1]);
//System.out.println(result[2]);
}
}
for(int i = 0; i < packages.size(); i++)
{
System.out.println(packages.get(i));
}
}
}
这里是文本文件(格式是故意的):
,123-ABC-4567,15 W. 15日圣,50.1
456-BGT-79 876,22百老汇,24
GAS-456 QWER,100东街20号,50
Q2Z-457-QWER,200东街20号,49
678-FGH-9845,,45第五大道,, 12.2,
678-FGH-9846,45第五大道,12.2
123-A BC-9999,46富酒吧,220.0
347-POY-3465,101 B'way,24
,123-FBC-4567,15月15日西圣,50.1
678-FGH-8465 45第五大道12.2
眼看你的数据,其中部分线路开始与不需要的逗号,并有多个逗号作为分隔符,一行几行的模式,甚至没有任何逗号分隔符,而是空间分隔符,你将不得不使用处理所有这些正则表达式行为。你可以使用这个表达式该做这一切为您的数据并适当地捕捉。
([\w- ]+?)[ ,]+([\w .']+)[ ,]+([\d.]+)
下面是上述正则表达式的解释,
([\w- ]+?)
- 捕获它由文字字符连字符和空格,并将其放置到第1组的ID
数据[ ,]+
- 这作为一个分隔符,其中它可以是一个或多个空格或逗号([\w .']+)
- 这抓住它由文字字符,空间和address
,并将其放置在第2组.
数据[ ,]+
- 再如上所述的分隔符([\d.]+)
- 这捕获待由数字和weight
的,并将其放置在第3组的.
数据下面是修改的Java代码就可以使用。我已经删除了一些变量声明的,你可以让他们回来需要。此代码打印捕捉你想要使用Matcher
对象的方式后,所有的信息。
Pattern p = Pattern.compile("([\\w- ]+?)[ ,]+([\\w .']+)[ ,]+([\\d.]+)");
// Reads the file
try (BufferedReader br = new BufferedReader(new FileReader("data1.txt"))) {
String str;
while ((str = br.readLine()) != null) {
Matcher m = p.matcher(str);
if (m.matches()) {
System.out.println(String.format("Id: %s, Address: %s, Weight: %s",
new Object[] { m.group(1), m.group(2), m.group(3) }));
}
}
}
打印,
Id: 456-BgT-79876, Address: 22 Broadway, Weight: 24
Id: QAZ-456-QWER, Address: 100 East 20th Street, Weight: 50
Id: Q2Z-457-QWER, Address: 200 East 20th Street, Weight: 49
Id: 678-FGH-9845, Address: 45 5th Ave, Weight: 12.2
Id: 678-FGH-9846, Address: 45 5th Ave, Weight: 12.2
Id: 123-A BC-9999, Address: 46 Foo Bar, Weight: 220.0
Id: 347-poy-3465, Address: 101 B'way, Weight: 24
Id: 678-FGH-8465, Address: 45 5th Ave, Weight: 12.2
让和我知道这对你的作品,如果你再有任何查询。
最后一行只包含一个令牌。因此分裂只会有一个元素返回数组。
的最小再现例如:
import java.io.*;
class Project1 {
public static void main(String[] args) throws Exception {
//Provide the file path
File file = new File(args[0]);
//Reads the file
BufferedReader br = new BufferedReader(new FileReader(file));
String str;
while ((str = br.readLine()) != null) {
if (str.trim().length() > 0) {
String[] result = str.trim().split("\\s*,\\s*", 3);
System.out.println(result[1]);
}
}
}
}
有了这个输入文件:
678-FGH-8465 45 5th Ave 12.2
输出看起来是这样的:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Project1.main(a.java:22)
Process finished with exit code 1
所以,你必须决定,你的程序应该在这种情况下做什么。你可能会忽略这些行,打印错误,或者只在您的列表中的一个添加的第一个标记。
你可以在你的代码添加以下代码
if (result.length > 0) {
packages.add(result[0]);
}
if (result.length > 1) {
addresses.add(result[1]);
}
if (result.length > 2) {
weights.add(result[2]);
}