#Error when trying to delete a row from a text file by entering a parameter?

105 messages · Page 1 of 1 (latest)

ebon kelpBOT
#

@silk plinth

kmusial46 Uploaded Some Code

I have attached a screenshot of the error can anyone help?

Uploaded these files to a Gist
silk plinth
opaque monolith
#

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

silk plinth
#

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)) {

opaque monolith
#

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...
silk plinth
#

should i create a constructor with these in it?

#

sorry im not that experienced in java 😅

opaque monolith
#

Well, what is it supposed to do with that information?

silk plinth
#

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

opaque monolith
#

In the file you're reading it from? It's already in that file

silk plinth
#

not sure i follow when i add a student the file is updated with either ft/pt and the fees are added on

opaque monolith
#

Oh wait, why is your Student constructor empty? The one that accepts a String for dob

opaque monolith
silk plinth
#

yea adding the student works and so does updating both files its just deleting it is what is throwing me an error

opaque monolith
# silk plinth

When you ran the program here, did you ever add students manually or only read from the file?

silk plinth
#

i added a student which worked fine and i tried to delete the same one afterwards

#

just randomly got this error now?

opaque monolith
#

Weird. After you define parts, print out line and parts.length so we can see what's up

silk plinth
#

do you mean inbetween string[] and string name?

#

or after double tuitionFee?

opaque monolith
#

After the line String[] parts = line.split(",");
Add 2 new lines for printing out those variables. I'm just experimenting here

#

So yes

silk plinth
#

not sure if thats what you mean

opaque monolith
#

I meant to print line, not parts

silk plinth
#

so just replace parts with line yes?

opaque monolith
#

Yes

#

We're doing this so we can see what the line looks like, so we know why parts is only reading one part

silk plinth
#

yes i understand now

opaque monolith
#

Ah, so the problem is there's an extra blank line

#

In the file

silk plinth
#

oh right yes

opaque monolith
#

You can remove the prints now

#

Does fixing the file fix that?

silk plinth
#

yes

#

deleting still doesnt work though

silk plinth
#

no not sure what to do

opaque monolith
#

The same thing as the other constructor, but turn the string into a Date first

silk plinth
#

i have a constructor for a new student just above that with the date format

#

not sure why i have 2

opaque monolith
#

Oh

#

Wait, you have the one for a Date dob, but the String dob is empty

silk plinth
#

yes

opaque monolith
#

loadDetails() uses the String dob constructor, which currently creates an empty Student

silk plinth
#

so will i change that to date?

opaque monolith
#

That would be easier. Assuming you mean inside loadDetails()

silk plinth
#

yes

#

its prompting me to change it back to string?

opaque monolith
#

wdym?

silk plinth
opaque monolith
#

Oh, why are you doing that? I meant change it to a Date object using the formatter as before

silk plinth
#

not sure im following

opaque monolith
#
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;
}
silk plinth
#

oh right yes sorry i thought u meant earlier in the chat

opaque monolith
#

No worries

silk plinth
#

this is what happens when i try to run it now

#

this is highlighted in red

#

its prompting me to create a constructor

opaque monolith
#

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

opaque monolith
silk plinth
#

yes i deleted the empty one so do i change dateOfBirth to dob in the loadDetails method?

opaque monolith
#

Only inside the new Student()

#

But yes

silk plinth
#

yes and what about the tuitionFee?

opaque monolith
#

Hm, whats it say when you click it?

#

Or hover

silk plinth
#

think i got it, it wasnt in the constructor at the start but it is greyed out

#

when i put it in

opaque monolith
#

Yep, just saw that

silk plinth
#

but now this is what im getting in the addStudent method prompting me to remove it

opaque monolith
#

Add the tuition fee back to the new student()

silk plinth
#

telling me to create a local variable?

opaque monolith
#

Did you remove the variable?

#

parts[6] right?

silk plinth
#

from loadDetails?

opaque monolith
#

Yes

silk plinth
#

did it there now also had to remove it from the constructor under it and the one at the start

opaque monolith
#

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

silk plinth
#

yes i understand now thank you

opaque monolith
#

👍

silk plinth
#

just seeing now when i try to add another student it overwrites the first one and the date is just a series of numbers?

opaque monolith
#

Using loadDetails?

#

Sorry I have to go to class rn, I'll be back in an hour or two

silk plinth
#

yea thats ok dont worry

#

its when im saving it so saveDetails

opaque monolith
silk plinth
#

this is what happens when i add a student and save it

#

the long number is supposed to be a date

opaque monolith
#

Try using dob.toString() + "," instead

silk plinth
#

yep that sorted it thanks

#

still overwriting the first student though

opaque monolith
#

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

ebon kelpBOT
#

@silk plinth

kmusial46 Uploaded Some Code
Uploaded these files to a Gist
silk plinth
#

sorry just seeing this now uploaded the code there

#

im having an error reading the file for some reason