Campbell Ritchie wrote:You haven't corrected the other two errors.
Campbell Ritchie wrote:Getting better I am afraid there are two serious logic errors are in that code. It isn't necessary to use valueOf() to turn a String into a String.
Campbell Ritchie wrote:💀@🕱☁☇¿!!⁇
Yuri Videz wrote:. . . it's another challenge . . . .
If anybody gives you such a “challenge” in real life, tell them that is a corrupted file and hit them. Hard. Remember that you can kill lots more people all at once with dangerous computing than with dangerous driving. Ask any 737Max8 pilot.
Tim M is right. Writing your own CSV parser is difficult and error‑prone enough without additional challenges like that.
Do all the lines have the comma missing there? What is happening is that you are reading “BS Entrep 1” as one token, which Knute has already pointed out. That is because it doesn't contain any commas, and you will then be trying to turn the token after the next comma, viz. “M” into a number, which won't work.
How many spaces are there going to be in that sort of text? Will it always be two? There is probably a way you can split that token using the String#split method and " " as your regex and telling it to miss out the first space. But, as others have told you, this is error‑prone and brittle code.
I have realised that it is possible to use one Scanner without changing delimiter because you can call nextLine() and ignore the returned value to move you onto the next line. You could also reset the Scanner to its default delimiter during the scanning of “BS Entrep 1” and use the delimiter with commas in after that. But it should be very obvious that we are in the realms of complicated code here. I presume you have read the documentation for Scanner? Have a look at the Java™ Tutorials about regular expressions.I missed that you wanted name all as one token. Sorry. My regex will separate the name into two parts. You could try using "\\\"\\s*,\\s*" as a delimiter just to finish reading the name, but that code is getting more and more complicated by the minute. I have an unjustified assumption that anything that complicated is wrong regardless!
Knute Snortum wrote:The problem with Campbell's solution is that it's not going to get that "data, with comma" should all be one token (chuck of data).
I think I would use a Scanner to read the file, not a buffered reader:-
When the Scanner scan reaches a line containing only spaces, the hasNext() call will return false and the loop will terminate before you have a chance to suffer any exceptions.