我当前有一个文本文件,其中包含以下内容:
1 Commercial & Enterprise 5 SLICE 59.99 IP MICRO
2 Commercial & Enterprise 5 SLICE 59.99 MULTI-USE SWITCH
.
.
.
.
18 Government & Military 6 TCP 15.00 TCP
我正在尝试拆分该行,以便我可以得到以下内容:
Product number: 18
Category: Government & Military
Product name: TCP
Units in stock: 6
Price: $15.00
Total value: $90.00
Fee: $4.50
Total value: $94.50
我目前有以下代码:
while ((line = lineReader.readLine()) != null) {
StringTokenizer tokens = new StringTokenizer(line, "\t");
p = new ActionProduct();
add(p);
String category = p.getCategory();
String name = p.getName();
category = tokens.nextToken();
int item = p.getItem();
double price = p.getPrice();
int units = p.getUnits();
while (tokens.hasMoreTokens()) {
item = Integer.parseInt(tokens.nextToken());
price = Double.parseDouble(tokens.nextToken());
units = Integer.parseInt(tokens.nextToken());
}
System.out.println("Category: " + category);
System.out.println("Product number: " + item);
System.out.println("Product name: " + name);
System.out.println("Units in stock: "+ units);
System.out.println("Price: $" + String.format("%.2f", price));
System.out.println("Total value: $" + String.format("%.2f",p.value()));
System.out.println("Fee: $" + String.format("%.2f", p.fee()));
System.out.println("Total value: $" + String.format("%.2f", value()));
}
我得到了这个输出:
Category: 1 Commercial & Enterprise 5 SLICE 59.99 IP MICRO
Product number: 0
Product name: null
Units in stock: 0
Price: $0.00
Total value: $0.00
Fee: $0.00
Total value: $0.00
Category: 2 Commercial & Enterprise 5 SLICE 59.99 MULTI-USE SWITCH
Product number: 0
Product name: null
Units in stock: 0
Price: $0.00
Total value: $0.00
Fee: $0.00
Total value: $0.00
所以我的问题是……我必须做什么来拆分生产线,以便我可以单独打印纺织品的每个值?提前感谢大家,非常感谢一些指导!
这是我的文本文件:
1 Commercial & Enterprise 5 SLICE 59.99 IP MICRO
2 Commercial & Enterprise 5 SLICE 59.99 MULTI-USE SWITCH
3 Commercial & Enterprise 4 SLICE 59.99 2100
4 Commercial & Enterprise 6 SLICE 59.99 IP
5 Commercial & Enterprise 4 HDX 45.00 HYBRID CARRIER
6 Commercial & Enterprise 10 TRANSip 45.00 IP Technology Suite
7 Commercial & Enterprise 5 GUI 30.00 LINK COMMAND SYS
8 Commercial & Enterprise 5 GUI 30.00 MAUI
9 Commercial & Enterprise 6 RCP 20.00 RCP
10 Government & Military 5 SLICE 60.00 IP MICRO
11 Government & Military 5 SLICE 60.00 MULTI-USE SWITCH
12 Government & Military 4 SLICE 60.00 2100
13 Government & Military 6 SLICE 55.00 IP
14 Government & Military 4 HDX.C 35.00 HYBRID CARRIER
15 Government & Military 10 TRANSip 30.00 IP Technology Suite
16 Government & Military 5 GUI 20.00 LINK COMMAND SYS
17 Government & Military 5 GUI 20.00 MAUI
18 Government & Military 6 TCP 15.00 TCP
最佳答案
仔细看看数据。您是否正在获取更多数据,或者这是唯一的文件?
如果您获得更多数据,则需要某种 if 规范,这样您就可以确定您的解析器将继续工作。
如果您有固定的数据定位,那么您可以使用
String part = line.substring(beginIndex, endIndex)
这个数据文件几乎是固定位置的,除非产品数量增加。
您可以尝试使用正则表达式或 line.split(delimitor)
在真正理解正则表达式之前,不要过多使用它们。
如果这是唯一的文件,我想我会从
String[] parts = line.split(" ") //two spaces
然后从得到的字符串数组中解析。
第一部分,parts[0],将包含产品编号和类别,但您也可以将其拆分。
关于java - 如何在读/写中拆分 tex 文件中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958986/