@silk plinth
#Error when trying to delete a row from a text file by entering a parameter?
105 messages · Page 1 of 1 (latest)
Looking at line 273 on the file you sent, it doesn't have equalsIgnoreCase(), weird
But I can tell you that the problem is you never put anything inside students. You're comparing students[i], but you never set that index of the array
just realised that the lines are wrong as i have comments at the top
public static void deleteStudent(Scanner scanner) {
if (numberOfStudents == 0) {
System.out.println("There are no students to delete.");
}
System.out.println("Enter the name of the student you wish to delete: ");
String studentDelete = scanner.nextLine().trim();
boolean studentDeleted = false;
for (int i = 0; i < numberOfStudents; i++) {
if (students[i].name.equalsIgnoreCase(studentDelete)) {
for (int j = i; j < numberOfStudents - 1; j++) {
students[j] = students[j + 1];
}
numberOfStudents--;
studentDeleted = true;
System.out.println("Student has been deleted successfully.");
break;
}
}
if (!studentDeleted) {
System.out.println("Student not found.");
}
}
line 273 is if (students[i].name.equalsIgnoreCase(studentDelete)) {
not sure i understand
I'll explain later, but first I'm confused here: ```java
while ((line1 = courseReader.readLine()) != null) {
String[] parts = line1.split(",");
String courseName = parts[0];
int studentFT = Integer.parseInt(parts[1]);
int studentPT = Integer.parseInt(parts[2]);
double totalFeesPaid = Double.parseDouble(parts[3]);
}
courseReader.close();
This part of your `loadDetails()` method doesn't do anything with those parts...
should i create a constructor with these in it?
sorry im not that experienced in java 😅
Well, what is it supposed to do with that information?
well studentft/pt is used for the report method
course name is final
and the fees is the tuition fees added from all the students
im just supposed to store that in the file
In the file you're reading it from? It's already in that file
not sure i follow when i add a student the file is updated with either ft/pt and the fees are added on
Oh wait, why is your Student constructor empty? The one that accepts a String for dob
Also you're right, sorry I just double checked and the student does exist, but not it's name
yea adding the student works and so does updating both files its just deleting it is what is throwing me an error
When you ran the program here, did you ever add students manually or only read from the file?
i added a student which worked fine and i tried to delete the same one afterwards
just randomly got this error now?
Weird. After you define parts, print out line and parts.length so we can see what's up
After the line String[] parts = line.split(",");
Add 2 new lines for printing out those variables. I'm just experimenting here
So yes
not sure if thats what you mean
I meant to print line, not parts
so just replace parts with line yes?
Yes
We're doing this so we can see what the line looks like, so we know why parts is only reading one part
oh right yes
Did you ever fix this
no not sure what to do
The same thing as the other constructor, but turn the string into a Date first
i have a constructor for a new student just above that with the date format
not sure why i have 2
yes
loadDetails() uses the String dob constructor, which currently creates an empty Student
so will i change that to date?
That would be easier. Assuming you mean inside loadDetails()
wdym?
Oh, why are you doing that? I meant change it to a Date object using the formatter as before
not sure im following
String dateOfBirth = parts[1];
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date dob;
try {
dob = dateFormat.parse(dateOfBirth);
}
catch (ParseException e) {
System.out.println("Error reading file");
return;
}
oh right yes sorry i thought u meant earlier in the chat
No worries
this is what happens when i try to run it now
this is highlighted in red
its prompting me to create a constructor
I'm assuming you deleted the empty String dob constructor? Anyway, you're supposed to use a Date, like the one we created, not the string
So after doing this use dob instead of dateOfBirth when creating the student
yes i deleted the empty one so do i change dateOfBirth to dob in the loadDetails method?
yes and what about the tuitionFee?
think i got it, it wasnt in the constructor at the start but it is greyed out
when i put it in
Yep, just saw that
but now this is what im getting in the addStudent method prompting me to remove it
Add the tuition fee back to the new student()
telling me to create a local variable?
from loadDetails?
Yes
did it there now also had to remove it from the constructor under it and the one at the start
You should have it in both the constructor and new student(), or neither. That's what matters
If it's in one, put it in the other
yes i understand now thank you
👍
just seeing now when i try to add another student it overwrites the first one and the date is just a series of numbers?
Using loadDetails?
Sorry I have to go to class rn, I'll be back in an hour or two
What do you mean by that last part?
this is what happens when i add a student and save it
the long number is supposed to be a date
Try using dob.toString() + "," instead
I'm home now so I can look into it
@silk plinth could you re-upload your code here? Just because we edited it a bit
@silk plinth