I'm trying to put in the soldPrice of a car that is sold in vehicle.csv. Here's a method for VehicleData class that i made.
the csv file have five column which is carPlate, carModel, acquiredPrice, carStatus(1 for not sold and 0 for sold) and soldPrice.
public static void updateVehicleData() {
Scanner input = new Scanner(System.in);
System.out.print("Please enter car number plate: ");
String carPlate = input.nextLine();
try {
RandomAccessFile raf = new RandomAccessFile("vehicle.csv", "rw");
while (raf.getFilePointer() < raf.length()) {
long currentPosition = raf.getFilePointer();
String line = raf.readLine();
String[] vehicleData = line.split(",");
// If the carPlate matches, update this row
if (carPlate.equals(vehicleData[0])) {
final int CARSTATUS = 0;
System.out.print("Please enter car sold price: ");
String soldPrice = input.nextLine();
// Create a new line with updated data
String updatedLine = carPlate + ","
+ vehicleData[1] + "," // Assuming you want to keep the car model
+ vehicleData[2] + "," // Assuming you want to keep the acquired price
+ CARSTATUS + ","
+ soldPrice;
// Move the pointer to the beginning of the line
raf.seek(currentPosition);
// Write the updated line, padding with spaces if needed
raf.writeBytes(String.format("%-" + line.length() + "s", updatedLine));
// Move the pointer to the end of the line
raf.seek(currentPosition + line.length());
raf.close();
break;
}
}
System.out.println("Successfully updated the vehicle data in vehicle.csv file.");
} catch (IOException e) {
System.out.println("An error occurred while updating vehicle data in vehicle.csv file.");
e.printStackTrace();
}
}
When I put the soldPrice for one of the cars, it became a bit messy in the csv file. The row under the vehicle soldPrice I modified went up and next to the soldPrice i changed. The carPlate of the messed up row also became one with the soldPrice and affected by the amount i put.
If I put in 10 as the soldPrice, instead of
...
PQR789,Chevrolet,121200,0,10
RST901,Mitsubishi,136700,0,153000
...
it became
...
PQR789,Chevrolet,121200,0,10ST901,Mitsubishi,136700,0,153000
...
The more 0 I add in soldPrice, the more carPlate of the Mitsubishi is erased. What can I do to make sure the csv file worked as it should.
To resolve this issue, the line separator should be added, after writing line in CSV file. In provided code, this line of code
raf.writeBytes(System.lineSeparator());
should be used after this line of code.
raf.writeBytes(String.format("%-" + line.length() + "s", updatedLine));
So, the code is as follows:
raf.writeBytes(String.format("%-" + line.length() + "s", updatedLine));
raf.writeBytes(System.lineSeparator());
Update: In provided code, the issue is there, when the length of replacement string is different than length of original string in file. When writeBytes() function is used to write "updatedLine", as the number of digits in "sold price" is increased, it overwrites that much length of next line, which is difference between length of "orginal line" and "updatedLine". To update file properly, the following code can be checked, according to requirements.
public static void updateVehicleData() {
Scanner input = new Scanner(System.in);
System.out.print("Please enter car number plate: ");
String carPlate = input.nextLine();
try {
RandomAccessFile raf = new RandomAccessFile("vehicle.csv", "rw");
boolean found = false;
while (raf.getFilePointer() < raf.length()) {
String line = raf.readLine();
String[] vehicleData = line.split(",");
if (carPlate.equals(vehicleData[0])) {
found = true;
break;
}
}
if (found == true) {
File tmpFile = new File("temp.txt");
RandomAccessFile tmpraf
= new RandomAccessFile(tmpFile, "rw");
raf.seek(0);
while (raf.getFilePointer()
< raf.length()) {
String updatedLine = raf.readLine();
String[] vehicleData = updatedLine.split(",");
if (carPlate.equals(vehicleData[0])) {
final int CARSTATUS = 0;
System.out.print("Please enter car sold price: ");
String soldPrice = input.nextLine();
updatedLine = carPlate + ","
+ vehicleData[1] + ","
+ vehicleData[2] + ","
+ CARSTATUS + ","
+ soldPrice;
}
tmpraf.writeBytes(updatedLine);
tmpraf.writeBytes(System.lineSeparator());
}
raf.seek(0);
tmpraf.seek(0);
while (tmpraf.getFilePointer()
< tmpraf.length()) {
raf.writeBytes(tmpraf.readLine());
raf.writeBytes(System.lineSeparator());
}
raf.setLength(tmpraf.length());
tmpraf.close();
raf.close();
tmpFile.delete();
}
System.out.println("Successfully updated the vehicle data in vehicle.csv file.");
} catch (IOException e) {
System.out.println("An error occurred while updating vehicle data in vehicle.csv file.");
e.printStackTrace();
}
}