-1

I just want to say thank you for taking the time to look into my question. I am currently working on an inventory interface. The issue I am having is that when I read an update file, to update my database file, this is what I get: database: abc,123,10.0,5.0,false,10 (partName,partNumber,listPrice,salePrice,onSale,quantity)

after I read in the update file: abc,123,10.0,5.0,false,10 is first in my db followed by abc,123,20.0,10.0,true,2

how do I fix this so that it updates the string in the db file, instead of appending it to the file?

here is some of my code:

//findBp method
public static BikePart findBp(ArrayList<BikePart> bpal, BikePart bp) {
    BikePart found = null;
    for(BikePart b : bpal) {
        if(b.getPartName().equals(bp.getPartName()) && b.getPartNumber() == bp.getPartNumber()) {
            found = b;
            break;
        }
    }
    return found;
}

//sort by partName
public static void sortName(ArrayList<BikePart> bpal) {
    Collections.sort(bpal, BikePart.bpNameComp);
}
//sort by partNumber
public static void sortNumber(ArrayList<BikePart> bpal) {
    Collections.sort(bpal, BikePart.bpPartNumComp);
}

//readFile method
public static void readFile(String fileName, ArrayList<BikePart> bpal) {
    if(fileName == null || fileName.equals("")) {
        return;
    }
    File file = new File(fileName);
    try {
        Scanner read = new Scanner(file);
        while(read.hasNextLine()) {
            String line = read.nextLine();
            String pv[] = line.split(",");
            BikePart bp = BikePart.toObject(pv);
            BikePart found = findBp(bpal, bp);
            if(found == null) {
                bpal.add(bp);
            } else {
                found.setQuantity(found.getQuantity() + bp.getQuantity());
                found.setPartName(bp.getPartName());
                found.setPartNumber(bp.getPartNumber());
                found.setListPrice(bp.getListPrice());
                found.setSalesPrice(bp.getSalesPrice());
                found.setPartOnSale(bp.isPartOnSale());
            }
        }
        read.close();
    }
    catch (FileNotFoundException e) {
        System.out.println(fileName + " is not found!");
        System.out.println("Enter another file name.");
        String fileName2 = in.nextLine();
        readFile(fileName2, bpal);
    }
}
//writeFile method
public static void writeFile(String fileName, ArrayList<BikePart> bpal) {
    try {
        BufferedWriter outStream = new BufferedWriter(new FileWriter(fileName, true));
        for(BikePart bp : bpal) {
            outStream.write(bp.toString());
            outStream.newLine();
        }
        outStream.close();
    }
    catch (IOException e) {
        System.out.println("file not found!");
    }
}

public static void main(String[] args) {
    //calendar field
    Calendar cal = Calendar.getInstance();

    //ArrayList for DB
    ArrayList<BikePart> bpal = new ArrayList<BikePart>();
    readFile("DB.txt", bpal);

    //user input variable
    String usrIn = "";

    //loop for user choice
    while(usrIn != "Quit") {
        //prompts user to select choice
        System.out.println("Please select your option from "
                + "the following menu: ");
        System.out.println("Read: Read an inventory delivery file");
        System.out.println("Enter: Enter a part");
        System.out.println("Sell: Sell a part");
        System.out.println("Display: Display a part");
        System.out.println("SortName: Sort parts by part name");
        System.out.println("SortNumber: Sort parts by part number");
        System.out.println("Quit: ");
        System.out.println("Enter your choice: ");

        //initiates usrIn
        usrIn = in.nextLine();

        //switch for input choice
        switch(usrIn) {

        case "Read":
            //read method 
            System.out.println("Enter file name: ");
            String fileName = in.nextLine();
            readFile(fileName, bpal);
            break;
        case "Enter":
            //enter method
            break;
        case "Sell":
            //sell method
            break;
        case "Display":
            //display method
            break;
        case "SortName":
            //sortName method
            break;
        case "SortNumber":
            //sortNum method
            break;
        case "Quit":
            //quit method
            writeFile("DB.txt", bpal);
            System.out.println("good bye! ");
            break;
        }
        if(usrIn.equals("Quit")) {
            break;
        }
    }

}

I haven't filled in the rest of the code yet, because I want to fix this issue first before I move on. I just want to say thanks again for taking a look.

kingdaro
  • 11,528
  • 3
  • 34
  • 38
Sameersan
  • 1
  • 4
  • Please do some debugging first to narrow down the problem instead of posting 100+ lines of code. – Henry Feb 18 '18 at 05:34

2 Answers2

0

Because you are appending to the file:

new FileWriter(fileName, true)

That's what the true parameter does.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • So if I set it from true, to false. Would it from appending? Because currently it has the old string and and the new string in my file when I write it. – Sameersan Feb 18 '18 at 09:26
  • I removed the true. And it works fine now! Thank you so much! – Sameersan Feb 18 '18 at 19:43
-1

In order to replace data in your file, you must read it, replace the data and write the new data out.

One way to do this is as illustrated in this answer. Which is briefly outlined below:

    // Read all the data
    BufferedReader file = new BufferedReader(new FileReader("data.txt"));
    String line;
    StringBuffer inputBuffer = new StringBuffer();

    while ((line = file.readLine()) != null) {
        inputBuffer.append(line);
        inputBuffer.append('\n');
    }
    String inputStr = inputBuffer.toString();

    file.close();

   // Replace your string code goes here.....

  // Then write all the updated data back

    FileOutputStream fileOut = new FileOutputStream("notes.txt");
    fileOut.write(inputStr.getBytes());
    fileOut.close();

Regarding the duplication:

It might be a case of how you update your ArrayList. May I suggest that you print the list before writing the file to check that it is not a duplicate entry? If it is then it is not an file-writing issue but a data updating issue. Then you must ensure that you are updating the BikePart in the list instead of adding it again.

TM00
  • 1,330
  • 1
  • 14
  • 26
  • I had created a method above that checks to see if the array list contains the given part. The method does work, it returns null if the part is not in the list or returns the part if it is in the list. So I run that check in the readFile method. But it still seems to add the part as if it’s a new part. It just appends it rather than replacing the old one. – Sameersan Feb 18 '18 at 09:29
  • Then you should read the complete file and rewrite the complete file as in my example. Does that work? – TM00 Feb 18 '18 at 10:01
  • The code is running great now! The bug was in the writeFile method. Initial what I had was: BufferedWriter outStream = new BufferedWriter(new FileWriter(fileName, true)); now I removed the true from that code snippet. And it works fine! – Sameersan Feb 18 '18 at 19:42