• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reading a a text file with java and tokenize to get aan averageScore for students. But I am getting

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So first I made a text file called : Grades.txt  that is located in src folder. Here's the what it has :
87,79,91,82,94
72,79,81,74,88
94,92,81,89,96
77,56,67,81,79
79,82,85,81,90

Then I made a class called TestScoreReader :



But I get errors during compilations times :



The output should be :
Average student for student 1 is 86.6
Average student for student 2 is 78.8
Average student for student 3 is 90.4
Average student for student 4 is 72.0
Average student for student 5 is 83.4

I tried hard to understand what is wrong with it!. I had to put the full path : src/Grades.txt or it complains that it cannot find the file. So far from the errors I understand that it has a problems in the formatting process but I cannot find how to make it right. Thanks in advance for your suggestions and help.
 
Saloon Keeper
Posts: 11068
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you post formatted text like source code or error message, please enclose them in CodeTags <-- link.

I'll fix it for you this one time only.

And welcome to the Ranch.
 
Carey Brown
Saloon Keeper
Posts: 11068
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't looked at it in detail but right off the bat this is a problem
You file does not have a space on either side of the comma.
 
Fred Masen
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Carey thanks but  I tried without spaces like that : (",") and I had the same errors . xception in thread "main" java.lang.NumberFormatException: For input string: "87"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at TestScoreReader.getAverage(TestScoreReader.java:37)
at TestAverages.main(TestAverages.java:25)
 
Fred Masen
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you guys have any more suggestions regarding my program , I would appreciate deeply So far I tried :  String[] tokens = line.split(","); as suggested but it did not work. Thanks a lot again!
 
Carey Brown
Saloon Keeper
Posts: 11068
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm at a loss. I cut and pasted your .txt file and code into my Eclipse environment, fixed the split() call, and everything worked as it should.

Is your data file corrupted?

"87" should parse ok unless there's an unprintable character hidden in there.
 
Fred Masen
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey I am so grateful for your answer! You are absolutely correct . I am under the Linux Mint Mate environment and I was using the default text editor. I also use Eclipse and in the past I use Gedit with great result so I went and go head installed Gedit and copied and pasted the txt file into it and as you said it 's working fine !!! However in the default editor I did saved as txt but as you mentioned something was wrong or corrupted! .  Thanks again!
 
Marshal
Posts: 80673
478
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.”.
 
Fred Masen
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell for your constructive observations. I barely know how to code you know! I am kind of an old guy and keep giving up learning Java especially when I am getting into situations like the code that I just posted. So far I gave up 3 times in the past year and I am back learning again the past month. I haven't studied the try and catch features yet. Regarding FileNotFoundException it was suggested by Eclipse before I decide to indicate the full path "src/Grades.tx and yes I could have delete that exception after that. Thanks for all the other suggestions that I will try to follow. I was always leaving spaces between lines of code for better visibility but I understand now that it could make stuff harder Eclipse for during compilation times. Although I am using the Oracle JDK 10.
 
Carey Brown
Saloon Keeper
Posts: 11068
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell, a nit-pick:
Should use hasNextLine() instead of hasNext(). hasNext() can return true when there isn't a full line yet.
 
Campbell Ritchie
Marshal
Posts: 80673
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but hasNextLine() will return true when you get to the last line even if the file ends with a blank line.
You are always dependent on a data file having the correct format for each line, so if there is an incomplete line you can blame it on the data file.
reply
    Bookmark Topic Watch Topic
  • New Topic