I have a text file “test.txt” in which I store student details such as I.D number and course taken. I want new students to be added immediately before the line that with “LASTSTUDENTEENTRYLINE”. The problem I have is that I’m collecting the student details in an array list called “objectInputFieldsList”. I’ve been trying to replace “LASTSTUDENTEENTRYLINE” with “objectInputFieldsList” + “LASTSTUDENTEENTRYLINE”. The problem is that the two don’t mix – one is an array list and the other a string. The array list contains input from text field values from users. How can I approach this successfully and replace LASTSTUDENTEENTRYLINE with “objectInputFieldsList” + “LASTSTUDENTEENTRYLINE” ? Thank you all so much in advance.
Here’s what I’ve been trying so far:
Sample text file before update:
123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES
Classes | 123 | English
Classes | 456 | Bilogy
LASTSTUDENTEENTRYLINENSUBJECTS
After an update with a new user the text file should look as:
123 | Oliver | Muchai
456 | Revilo | Chamu
678 | Eddys | Rockery
LASTSTUDENTEENTRYLINENNAMES
Classes | 123 | English
Classes | 456 | Biology
Classes | 678 | Kiswahili
LASTSTUDENTEENTRYLINENSUBJECTS
The Code:
// The ArrayList called from the class that gets the user input from the JTextFields
AddNewClientSaveAction save = new AddNewClientSaveAction();
StringBuffer sb = new StringBuffer();
String lastNames = "LASTSTUDENTEENTRYLINENNAMES ";
String lastSubject = "LASTSTUDENTEENTRYLINENSUBJECTS";
String textToEdit1 = lastNames;
int cnt1 = sb.indexOf(textToEdit1);
String replacementString1 = "\n" + save + "\n" + lastNames;
sb.replace(cnt1,cnt1+textToEdit1.length(), replacementString1);