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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Knute Snortum
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Ganesh Patekar
• Frits Walraven
• Tim Moores
• Ron McLeod
• Carey Brown
Bartenders:
• Stephan van Hulst
• salvin francis
• Tim Holloway

Greenhorn
Posts: 1
Hi, I need some assistance.
I have to print out the average exam score and how many students achieved the specified grade. I'm stuck trying to get the number of students for the specified grade.
The printout should mirror the following:

What grade would you like to count? B
How many students are in the class? 10

Exam score for student 1: 85
Exam score for student 2: 66
Exam score for student 3: 95
Exam score for student 4: 80
Exam score for student 5: 78
Exam score for student 6: 90
Exam score for student 7: 89
Exam score for student 8: 54
Exam score for student 9: 72
Exam score for student 10: 100

Average exam score is 80
3 students got a B

This is what I have so far: Can someone assist? Thanks:) Jessica

Saloon Keeper
Posts: 5161
54
• 1
What you're being asked to provide is known as a "histogram". In this approach you have a number of counters, one for each grade in your case. One count variable will not suffice. When you determine the letter grade increment that grade's counter.

You could clean up your if()'s if you use "else if"

Sheriff
Posts: 12748
210
Sorry, but what's being asked doesn't really fit the definition of a histogram as far as I know. It's a nice little twist to a problem that normally involves a histogram though, and one that's arguably simpler.

A few things:

1. Scanner has a nextInt() method you can use to avoid jumping through Integer parsing hoops. For some reason, you jumped through that hoop on line 24 to get the value for numStudents. On line 30, you didn't. Be consistent and prefer the simpler way; don't jump through hoops if you don't need to.

2. You declared the variables A, B, C, D, F but it doesn't look like you ever use them.

3. Indentation is important because it gives you visual clues about the flow of logic. Lines 35-55 should be less indented and lined up with the lines 28, 32, and 33.  If you're using an IDE, figure out what the shortcut keys are for autoformatting your code, then use it. In Eclipse, it's Alt+Shift+F.  If you're not using an IDE, there are online code beautifiers available (for example, https://www.tutorialspoint.com/online_java_formatter.htm)

Think about your logic a little bit. The program asks the user to enter a letter grade. Then, you're supposed to count how many students got that particular letter grade. If you look at the first thing you do in each of those if-statements you have stacked up on top of each other, you're actually clobbering the letter that the user entered.  That is, if the user says they want to count how many students got an A, then when you enter the number grades, you enter a 70, the first thing you do is clobber the 'A' in the variable letter (the value that the user entered) by changing it to a 'C'. That's not right.

Talk yourself through the manual process first. How would *you* solve this problem if you only had pen and paper? Once you can describe the process as you would do it, it's easier to tell the computer how to do it.

Junilu Lacar
Sheriff
Posts: 12748
210
Also, that stack of if-statements is not where it should be. Right now you've written them outside of the for-loop. Again, talking yourself through the process and doing it manually will give you a better idea of where a stack of if-statements like that would go. Of course, you have to change the logic inside each of those if-statements since what you have there now is also incorrect.

Junilu Lacar
Sheriff
Posts: 12748
210
... and Welcome to the Ranch!

Marshal
Posts: 61774
193
Welcome again

You are right to have A B C... in order. But have you not been introduced to multiple if‑elses? They make that sort of thing much neater, and are less error‑prone. You have to be sure that the numbers at the end of each if match those at the beginning of the next. I see Carey has mentioned that already. But I like to avoid >= and <=. And depending on which way you are going (up or down) you don't have to specify the ranges. Only a boundary value. Look here.

Junilu Lacar
Sheriff
Posts: 12748
210
After running your code through a beautifier and adding some spacing, we get this:

This makes it easier to see that each if-statement is independent of all the other if-statements you have stacked up. What does this mean? Well, since you're checking the same variable, score, it's a strong indication that you have a logic bug. You really should only execute one of these if statements. However, the structure of the code you wrote makes it possible for multiple if-statements to be executed. Because you have logic errors in your if-statement conditions, this is exactly what happens.

For example, if the score entered is 89, the if-statement on line 35 is executed. Logically, you wouldn't want any of the other if-statements to be executed. However, when you get to line 41, the condition is still met because 89 is between 79 and 90. So that if-statement is executed as well. This produces incorrect results. The fix to this problem has been explained by Campbell and Carey: use a series of cascading if-else-if statements and fix your boundary conditions.