1
テキストファイルのデータを読み込み、クラスオブジェクトに配列として分割しようとしています。私は、クラスProductRecordsを持っています。このファイルからテキストファイルを読み込んでJavaのクラスオブジェクトに格納する方法
class ProductRecord
{
public String productCode;
public String category;
public String description;
public int aisleNumber;
public int currInventory;
public ProductRecord(String code, String category, String description, int number, int currInventory)
{
this.productCode = code;
this.category = category;
this. description = description;
this.aisleNumber = number;
this.currInventory = currInventory;
}
public String getProductCode() {
return productCode;
}
public String getCategory() {
return category;
}
public String getDescription() {
return description;
}
public int getAisleNumber() {
return aisleNumber;
}
public int getCurrInventory() {
return currInventory;
}
}
と私は読んしようとしています
Thornton Hardware Store
7
P24-Qaa-1354 "hammer" "Bailey one-piece steel ripping hammer" 6 2
P24-Qbw-2495 "hammer drill" "Holsinger hammer drill, 8.5A 1/2-in" 7 1
P33-Qes-4782 "screwdriver" "Bailey 18-piece screwdriver set" 5 2
P25-Taa-1244 "nail" "Yolen framing nails, 50-count 3-1/4-in 28-degree" 6 20
P25-Tab-3509 "nail" "Yolen finish nails, 1000-count 18-guage 2-in" 6 9
P25-Tab-3506 "nail" "Yolen finish nails, 1000-count 16-guage 2-1/2-in" 6 14
P25-Tac-3672 "nail" "Yolen roofing nails, coil 1-1/4-in" 8 5
LC "nail"
LC "plant"
LP P24-Qbw-2495
LP P62-Aaa-1387
S P25-Tab-3506 5
S P24-Qbw-2495 1
R P25-Tab-3509 10
最初の情報は、店舗の名称であり、第二は、インベントリ内のアイテムの合計数です。私はそれを分割しようとしたときに3行目の例えばクラス
P33-Qes-4782 "screwdriver" "Bailey 18-piece screwdriver set" 5 2
P33-Qes-4782 --> productCode
"screwdriver" --> category
"Bailey 18-piece screwdriver set" --> description
5 --> aisle number
2 --> current inventory
に格納されるので、それは、カテゴリと説明の間に空白を分割し、私はそれを望んでいません。ここに私のコードです。
スキャナを使用して中括弧の空白に影響を与えずにこれを分割する方法はありますか? String[] tokens = file.nextLine().split("\"")
代わりのString[] tokens = file.nextLine().split(" ")
を使用することにより
Scanner input = new Scanner(System.in);
System.out.println("Enter the input fileName: ");
String fileName = input.nextLine();
try {
Scanner file = new Scanner(new File(fileName));
System.out.println();
//number of products in the inventory
String inLine = file.nextLine(); // stores the name of the store
String line = file.nextLine();
int numberOfItems = Integer.parseInt(line);
System.out.println(numberOfItems);
System.out.println("*************************");
while (file.hasNextLine()) {
String[] tokens = file.nextLine().split(" ");
//String[] tokens = file.nextLine().
for (int i = 0; i < 5; i++) {
System.out.println(tokens[i]);
//System.out.println("\n");
}
System.out.println("\n");
}
/*ProductRecord[] records = new ProductRecord[file.nextInt()];
records[0].productCode = file.next();
System.out.println(records[0]);*/
/* for(int i =0; i < numberOfItems;i++)
{
//records[i] = new ProductRecord([])
}*/
} catch (IOException ioe) {
System.out.println("file not found");
}