[Solved] [Java] Error in Code & How to Improve My Code..? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[Solved] [Java] Error in Code & How to Improve My Code..?

I'm just practicing what I've learned in Java so far and I came up with a problem then solved it. So, the reason for this post is: 1.) I'm having two errors when I run it and I can't seem to find the issue. 2.) I was wondering if there are better ways to go about what I did and if so, how? Please note: - I've only completed the arrays part in the tutorial and thus may not know some of the more advanced ways you would normally use. - I tried organizing it using more classes but it only seemed to make it more disorganized/less efficient. So I went back to my original version that I've linked to this post. Code: https://pastebin.com/1cHWQcT3 Java Error: https://pastebin.com/UUd2tjB1

12th May 2018, 7:20 PM
Tommy
Tommy - avatar
10 Answers
+ 4
Your for loop isn't correct for how you are using it. i will contain 30, 60, 85, 50, 90, 40, 90, 75, 99, 54 in order. Replace: for (int i: grades) with: for (int i = 0; i < grades.length; i++)
12th May 2018, 9:29 PM
John Wells
John Wells - avatar
+ 4
I have it running here. Your totals per grade used the old for loop i correctly. https://code.sololearn.com/c1KONsgN4p3M
12th May 2018, 9:57 PM
John Wells
John Wells - avatar
+ 3
https://code.sololearn.com/c22l8F2nKOan/?ref=app what above sir describe you about for each loop . you have to understand that.. i correct your program let me if you not understand happy coding(-_-)
12th May 2018, 9:56 PM
Arun Tomar
Arun Tomar - avatar
+ 3
Your count increments are using i which never gets above F use grades[i] to fix it. I will have a hugh improvement code for you to learn from soon.
12th May 2018, 11:00 PM
John Wells
John Wells - avatar
+ 3
Here is how I'd code this: https://code.sololearn.com/cV3E3e5A9lAE
12th May 2018, 11:03 PM
John Wells
John Wells - avatar
+ 2
@John Wells, ohh okay. Thanks a ton. Thanks for your improved version as well. I don't fully understand it yet with my current knowledge but I'll save it and look at it later. ------------------------------------------------------- @Arun Tomar, thank you for your replies as well. Unfortunately, I am having trouble understanding you. I understand it's very hard to explain something in a second language, so I'm definitely not criticizing you. Just that I don't understand very well. Sorry.
12th May 2018, 11:12 PM
Tommy
Tommy - avatar
+ 1
@John Wells, thank you for your response. Why wouldn't an enhanced for loop work for this situation? In addition, strangely, changing that loop does fix the errors but outputs: ... There is 0 A grade. There is 0 B grade. There is 0 C grade. There is 0 D grade. There are 10 F grades. Yet your link appears to be the same and outputs: ... There are 3 A grades. There is 1 B grade. There is 1 C grade. There is 1 D grade. There are 4 F grades. What am I missing? Why are the outputs different? I only changed the for loop like you said to. Code: https://pastebin.com/Avz1T4Tu
12th May 2018, 10:49 PM
Tommy
Tommy - avatar
+ 1
@Donna, I tried your comment as well. It does fix the error but has the same result as I told John Wells about. New Code: https://pastebin.com/ACeS8g8E (All in Main class) Using Eclipse, it outputs: ... There is 0 A grade. There is 0 B grade. There is 0 C grade. There is 0 D grade. There are 10 F grades. What it should output: There are 3 A grades. There is 1 B grade. There is 1 C grade. There is 1 D grade. There are 4 F grades. Why are the outputs different?
12th May 2018, 10:56 PM
Tommy
Tommy - avatar
+ 1
Tommy@ you also change in.. if else if statement see in my code or john wells code. yoi got your ans
12th May 2018, 11:04 PM
Arun Tomar
Arun Tomar - avatar
+ 1
Tommy updated to add comments to explain things some.
12th May 2018, 11:39 PM
John Wells
John Wells - avatar