Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!

Yuri Videz

Greenhorn
+ Follow
since May 15, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Yuri Videz

Sorry I didn't reply yesterday. My town was hit with heavy rains that cut off my power.

Thank you for correcting me, I'll find a way to implement what you said
1 week ago

Campbell Ritchie wrote:You haven't corrected the other two errors.



Sorry for the late reply. What were the errors? I removed the String.valueOf around them

1 week ago

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.



Thank you! I adjusted that
1 week ago
Update: I realized I was getting an out of bounds error because the length of the Program String varied. I was able to get it to output everything right by using the code below with no errors:



All I have to fix now is the sorting of the List of students I'm new to that too, but I'll study how to do it as best as I can. Thank you so much for your time and help! You all helped me go in the right direction for this one.
1 week ago

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.

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 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!




Thanks for your help!

Yes, our teacher isn't the best. He glosses over everything and basically leaves us to figure stuff out on our own, gets mad when we ask him questions, is really old-school etc--we're kind of teaching ourselves, here, while trying to meet his standards at the same time.

The name doesn't have to be all one token. The task is to sort Student objects by program, age, then passing or failing, which I can do.
Do you think Having the program String as "BS Entrep 1" would matter in that regard?

I came up with this to separate the year. Doing this put everything in place including age, gender, average, etc, but I get another strange OutOfBounds error although I counted the String's length, and it shouldn't be giving me an out of Bounds:



This is the error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 10
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:709)
at edu.slu.prog2.finals2.VidezStudentsRecords.readDataFileIntoList(VidezStudentsRecords.java:61)
at edu.slu.prog2.finals2.VidezStudentsRecords.run(VidezStudentsRecords.java:29)
at edu.slu.prog2.finals2.VidezStudentsRecords.main(VidezStudentsRecords.java:21)



I checked the length of the program string beforehand, and it was 11, which means I should be just fine with the .charAt(10) to get the year.

Thank you for your time. I can't stress how much I appreciate it. It's kind of new for me to find places where I can genuinely get help
1 week ago
Thank you! It's okay, I understand, thank you for giving me your time. I am happy to wait
1 week ago
Hello again!


I think I would use a Scanner to read the file, not a buffered reader:-



@Campbell Ritchie, Thank you! Your solution fixed the ArrayOutofBounds error. However, I'm getting a new one. When it comes to reading the year, I get the following:



I tried adjusting the Delimiters, but I don't quite understand how they work yet.


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.



How would I adjust the Delimiters to make this true?

2093505,"AMBROS, RALPH HENRY B",BS Entrep 1,M,23,93.94

BS Entrep should be read separately from 1--it's another challenge from our teacher.  

Thank you, again, for all your help. I tried asking the people over at StackOverflow but they were kind of condescending. I'll definitely use coderanch from now on.
1 week ago
Hello! I'm a new java programmer.

I'm using .substring to read a specific part of a string from a text file. Here's a sample of the string to be read:

2093505,"AMBROS, RALPH HENRY B",BS Entrep 1,M,23,93.94

We're supposed to ignore the quotes, which is why I'm using substring. When I run the program though, I get this error:



This is the code where the error is:



I caught the error using a try/catch before, I took it off to see where I went wrong.

I can print the details just fine. They show up in order and everything, but it still gives me the OutofBounds error for all of the details (id, name, year, etc). Where am I going wrong?

Sample output:




Thank you for any help you can give.
1 week ago