first time poster here. Thank you in advance for viewing my question. I'm having a ton of trouble with a homework problem in which I have to read a specific range of bytes from a RandomAccessFile, then check that range of bytes to see if they all equal 0. I've looked all over for something that pertains to this, but nothing I've found quite hits the spot. Any help provided will be appreciated.
The problem tells us that there is a certain file that contains data for hypothetical students in a school. Each of these students is represented in 40 bytes of code, but the first four bytes of our file must be an integer with the total number of students in the school(let's say there are 75). Bytes 4 through 43 represent the first student (#0), 44 through 83 represent the second (#1) and so on. When a student transfers to another school, their 40 bytes is overwritten with all 0's (characters).
I've written a method called "transferStudent" that takes a String which represents the file name and the integer which represents the number of students. If there are any exceptions or if the file doesn't overwrite the student's data for some reason, I return false;
Here is my work thus far:
public static Boolean transferStudent(String fileName, int studentNum) {
RandomAccessFile file = new RandomAccessFile(fileName, "rw");
file.writeInt(75);
try {
if (studentNum == 0) {
file.seek(4);
file.writeBytes("0000000000000000000000000000000000000000"); // 40 zero characters
file.seek(4);
for (int i = 0; i < 40; i++) {
if (file.read() == 0) {
return true;
}
}
return false;
}
else if (studentNum > 0) {
file.seek(4 + (studentNum * 40));
file.writeBytes("0000000000000000000000000000000000000000"); // 40 more zeroes
file.seek(4);
for (int i = (4 + (studentNum * 40)); i < (44 + (studentNum * 40)); i++) {
if (file.read() == 0) {
return true;
}
}
return false;
}
else {
return false;
}
}
catch (Exception e) {
return false;
}
}
Whenever I view the binary file that's been created, there are indeed 0's in the range that corresponds with the studentNum. However, the console always prints false - the check isn't working for some reason. I'm on the verge of tearing my hair out over this. Please help!