Welcome to the Ranch
You obviously know how to write code, so I am afraid you aren't going to like my suggestions.
First suggestion: Get your code formatting right. One blank line between successive methods. No blank lines inside methods No double spaces between tokens. Choose one indentation convention and stick to it religiously: look at lines 14 and 20. Code formatting isn't just something we mods fuss about; if you have bad formatting it will make the code hard to read, and it is you who will have the most difficulty with it. Don't use tabs for indentation; set up the option on yoru editor to convert a tab automatically to 4 spaces. No code after the { at the end of the line. Always use
{...} after
if. At least you know how to
avoid long lines
If we apply that to your code, we get this:-
Then I made my main class :
So far, so good. Doesn't that look better
Next suggestion you won't like:- Only declare IOException. There is no need to declare that a method might throw number format exception because that is an unchecked exception and the compiler will ignore it. There is no need to declare file not found exception because that is a subtype of IOException and the compiler will presume file not found is included in IOException.
Third suggestion which you won't like:- The file reading program is only doing half the job. It checks whether there is a line and reads it, but the repetition is done elsewhere. I would prefer something like this:-
That is incomplete code. Line 1: See
this Java™ Tutorials section. That ensures you close the Scanner and the file, without having to rely on users of the code calling the
close() method, which you aren't doing. Line 1 again: see
here in the Java™ Tutorials too about Paths.get. I think there is no need for the Scanner or the File to be fields; they can be local variables.
Lines 3 and 5 appear to contradict each other, but I think that is probably the right way to ensure you don't get an empty line, but line 6 will discard empty lines. The
strip() method is in Java11 only: for older versions use
trim() instead.
Line 5 assumes that
line is a field. Maybe it would more object‑oriented to create a StudentMark object from that line and let that process the line.
Line 12. I think that is the correct exception; if not, the compiler will tell you
Now you have to work out how to process those lines. I think there is probably a problem with the regex to split your line into an array. For debugging, try adding line 33½:-
That will show you what you are parsing, inside “quote marks”, so you can see exactly what is happening. Once you have worked that out, you should be able to work out whether the regex is working or not. I think that "," should work; if not, try "\\s*,\\s*". The
\\s part means whitespace, and the star allows any number of such characters including 0. So are allowed whitespace or no whtespace before and after the comma.
[edit]Add “about Paths.get.”.