我目前有一个文本文件,其中包含以下内容:
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