我現在有一個具有以下內容的文本文件:如何在讀/寫中分割tex文件中的一行?
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
RegEx是一個選項嗎? – David 2012-03-31 19:33:59
humm不,我必須使用FILE.txt – ShaunK 2012-03-31 19:41:23
??對不起,但你的回覆關於不能使用正則表達式由於不得不使用file.txt是沒有意義的。您是否熟悉正則表達式以及它們的用途?如果是這樣,請詳細說明爲什麼你不能在'String#split(...)' – 2012-03-31 19:45:55