Tôi đang đọc 2 tệp csv: store_inventory
& new_acquisitions
.
Tôi muốn có thể so sánh tệp store_inventory
csv với new_acquisitions
. 1) Nếu tên mục khớp với chỉ cập nhật số lượng trong store_inventory. 2) Nếu new_acquisitions có một mục mới không tồn tại trong store_inventory
, sau đó thêm nó vào store_inventory
.Java: Tệp CSV đọc & ghi
Đây là những gì tôi đã làm cho đến nay nhưng nó không phải là rất tốt. Tôi đã thêm nhận xét mà tôi cần thêm taks & .
Bất kỳ lời khuyên hay mã nào để thực hiện các tác vụ trên sẽ rất tuyệt vời! cảm ơn.
File new_acq = new File("/src/test/new_acquisitions.csv");
Scanner acq_scan = null;
try {
acq_scan = new Scanner(new_acq);
} catch (FileNotFoundException ex) {
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}
String itemName;
int quantity;
Double cost;
Double price;
File store_inv = new File("/src/test/store_inventory.csv");
Scanner invscan = null;
try {
invscan = new Scanner(store_inv);
} catch (FileNotFoundException ex) {
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}
String itemNameInv;
int quantityInv;
Double costInv;
Double priceInv;
while (acq_scan.hasNext()) {
String line = acq_scan.nextLine();
if (line.charAt(0) == '#') {
continue;
}
String[] split = line.split(",");
itemName = split[0];
quantity = Integer.parseInt(split[1]);
cost = Double.parseDouble(split[2]);
price = Double.parseDouble(split[3]);
while(invscan.hasNext()) {
String line2 = invscan.nextLine();
if (line2.charAt(0) == '#') {
continue;
}
String[] split2 = line2.split(",");
itemNameInv = split2[0];
quantityInv = Integer.parseInt(split2[1]);
costInv = Double.parseDouble(split2[2]);
priceInv = Double.parseDouble(split2[3]);
if(itemName == itemNameInv) {
//update quantity
}
}
//add new entry into csv file
}
Cảm ơn bạn một lần nữa. =]
Bạn sẽ tìm thấy bạn nhận được nhiều hơn và tốt hơn câu trả lời nếu bạn thực sự đặt một câu hỏi. –