• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to compute the average grade from a file.txt?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a project that reads a file containing students’ numerical grades–one grade per line. I did create the file.
I'm fine with opening the file but I'm struggling with how to get it to read the average of the first line then continue to all the other ones as well returning the value in the println. I know the result which is the average grade is B.
My program should not assume that I know how many lines of data are in the file.
The letter grade as a char value (A = 90-100,B=80-89,C=70-79,D=60-69,F=0-59). Any suggestion, Thanks.



 
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your file look like? Is it just one number per line, or is it more complex? Could you post a sample?
 
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't get a combination of nextLine() and nextInt() to work. Which you use depends on the format of the text in the file;as JJR said, please show us a typical line.
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My file looks like this :

A 90-100
B 80-89
C 70-79
D 60-69
F 0-59

Refered to the question which ask to create a file and then one grade per line. Is my file correct?
 
Rancher
Posts: 259
13
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can that file be correct, you don't have any student name, for who's grade does each line belong?
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is not students name just because I understand from the question "reads a file containing students’ numerical grades–one grade per line." So I don't think that I have to create a student name. Also the sample out show "The average grade is B."
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know whether that file is correct, but it doesn't look like a list of marks to be graded. Maybe there is a different file somewhere?
 
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I would expect a file with content like:

How else can I calculate an average?
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I didn't understand the assignment but I created my file from this sentence "after processing the entire file compute the average grade and return the equivalent letter grade as a char value (A = 90-100, B=80-89, C=70-79, D=60-69, F=0-59)", I think the file will be like this :
A = 90-100
B = 80-89
C = 70-79
D = 60-69
F = 0-59
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:But I would expect a file with content like:

How else can I calculate an average?



By using my code, if I use the file containing your data I got a average 51.
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdallah Taha wrote:. . . By using my code, if I use the file containing your data I got a average 51.

How did you manage that? The correct average is 60¼. If you use integer arithmetic, that will come to 60
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did like this :
 
Piet Souris
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean with: s.nextLine() and s.nextInt() perhaps a file of this form:

?
Beware that "s.nextLine();s.nextInt();s.nextLine();s.nextInt(); ..." is dangerous. Campbell already warned for that.

But if your file is in that form, then your calculation is wrong. If we take as average for grade A a score of 95, then the sum goes up by 23 * 95, and the number by 23. You cannot calculate an exact average with these data.

If your file is in the form:

then you code is okay (again, beware Campbells warning), but that file contains a lot of redundant information.
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Abdallah Taha wrote:. . . By using my code, if I use the file containing your data I got a average 51.

How did you manage that? The correct average is 60¼. If you use integer arithmetic, that will come to 60


Thanks for all, I found the correct average 60.25 with this :
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdallah Taha wrote:. . .

No, Don't use Integer.parseInt() with a Scanner. Use Scanner#nextInt().
Don't combine Scanner#next() and String#split(" ") because the next() call will return one token and that will only ever split into a one‑element array.
Please indent your code correctly; look at lines 5‑8.
Find out how to use a loop to get rid of anything not an int before finding the next int. Search my posts for “Rob Spoor” because Rob taught me how to do it.
 
Abdallah Taha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread is fascinating in that it shows just how difficult communicating an idea can be.

I am 100% certain Piet's original interpretation of what the input data file should look like was correct: "reads a file with students' numerical grade—one grade per line."

And yet there was still discussion and confusion about what the input file should look like after Piet's correct assessment.
 
Junilu Lacar
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdallah Taha wrote:Thanks for all, I found the correct average 60.25 with this:


You should consider that number as an example only. You will only get that number with specific sets of data. Do you have the data that is supposed to give you that result? If not, then it's highly unlikely you'll get those exact results with some random set of data.

Try running your program with simple sets of data.

For example, this:

10
20

Should give you an "F" because the average of those two numbers is 15. You'd also get the same results from this:

58
59
60

That's because the average of those three numbers is 59. I would test the program with various sets of data, each of which would cover a different scenario that the program should successfully handle. That is, have at least one test that would cover scenarios where the result should be one of the five possible letter grades. If you did your due diligence, you'd have a few test cases to cover the edge conditions, like when the average is 60 (grade should be "D") or when the average is 89 (the highest average you can get for a grade of "B").

The last thing you want to do is just chase down a known result and then stop testing your program when you get it. The worst thing you could do is try to chase down that result with incorrect input data and still get the result. That would mean your program was wrong.
 
Junilu Lacar
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdallah Taha wrote:Thanks for all, I found the correct average 60.25 with this:


What if the average turns out to be 59.6? What grade is that supposed to get?

The ranges for the letter grades you showed don't include values like 59.6:


A: 90 - 100
B: 80 - 89
C: 70 - 79
D: 60 - 69
F: 0 - 59


Since the ranges are specified like that, I would assume the average you're supposed to calculate should be whole numbers. That means you should be using an int instead of double for the average.
 
Bartender
Posts: 1899
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would consider creating a frequency distribution for graphically showing your results. This visual aid is easy to add and doesn't require any special graphics.

I would use asterisks: "*".

Having a graphical display is often more helpful -- more visually helpful -- than just seeing how many (in a number).

90-100: **
80-89:   *********
70-79:   ************
60-69:   ******
50-59:   **
 0-49:   *

Overall Average: 62.8%

-- mike
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic