• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this problem baffles me

 
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, so i'm just learning to program, and in class, we're working with arrays. however, i'm having a hard time with them in general, and now i'm having a weird program issue.

here is the first part of the assignment.


Write a Java program that does the following:

The file [scoreA] contains a sequence of floating-point numbers, each 0–100, representing how students fared in a class. There are no more than 100 grades in the file. The sentinel that signals the end of the values is -999.

1. Read the scores into an array.
2. Count those scores in “bins”, representing letter grades (A: ≥90, B: ≥80, C: ≥70, D: ≥60, F: <60).
3. Print how many students received each grade, e.g.

A: 4
B: 6
C: 5
D: 1
F: 0

so i wrote it and ran it with a file containing the following: 70.5 49.5 66.5 77 1 86 96 76.5 70.5 1 73.1 73.5 87 87.5 80 5 26.5 10 41 74 95 2.5 13 78.5 66 70 99 77 94.5 73.5 -999 (in that order). it reads and counts everything just fine except for the C grades. in the file, the actual count is A: 4, B: 4, C: 11, D: 2, F: 9. no matter what i do, it always tells me there are only 10 C grades. i must be doing something wrong, but i don't know what. here's the code.





i am a little baby programmer, and arrays are probably the most complex concept as i can understand (barely). can anyone tell me what i'm doing wrong?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Euridice, and welcome to the Ranch!

You're throwing away the first number read from the file, which happens to fall in the 'C' grade range.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Euridice!

(Great name by the way.) Welcome to Java Ranch! I believe Darryl's hint should be enough for you to solve your problem, but I have a couple of pointers for you about posting questions here that will ensure that people always read and respond to them. They're so common that we can link to them just by typing them as one word:

UseAMeaningfulSubjectLine
UseCodeTags


Using code tags is really easy and makes a big difference. Just hit the "Code" button at the top of the edit window. This time, I've added them to your post for you. Just call me Orpheus.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry about the title and coding. i'll remember that for next time.

thanks for the hint, Darryl! now it looks like this:




it works! the second part of my problem was the sentinel value. it counts -999 as a grade, so it tells me there are 10 F grades even though there are only 9. i have it written this way because the TA of my class walked us through that part. does the mean she missed something, or did i? it's happening because Java works through the first bad value and then stops, right? but i don't want it to count the sentinel, just acknowledge that it exists and terminate.

EDIT: sorry, i also have to print a bar graph for grade, so if there are 4 A grades, it should output A: 4 ####

the only way i could think to do this was use this, but it doesn't work:



it replaces star with a single pound sign even though star should be the value of A. maybe.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it works! the second part of my problem was the sentinel value. it counts -999 as a grade, so it tells me there are 10 F grades even though there are only 9. i have it written this way because the TA of my class walked us through that part. does the mean she missed something, or did i? it's happening because Java works through the first bad value and then stops, right? but i don't want it to count the sentinel, just acknowledge that it exists and terminate.


I ran your code using the input you specified. The output was 9 F grades - not 10.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

EDIT: sorry, i also have to print a bar graph for grade, so if there are 4 A grades, it should output A: 4 ####

the only way i could think to do this was use this, but it doesn't work:

view plaincopy to clipboardprint?

1. String pound = " #";
2.
3. String star = (String.valueOf(numA));
4. star = star.replace(star, pound);
5.
6. System.out.println("A: " + numA + star);

String pound = " #"; String star = (String.valueOf(numA)); star = star.replace(star, pound); System.out.println("A: " + numA + star);

it replaces star with a single pound sign even though star should be the value of A. maybe.


Instead of using

Look at using System.out.print(). On one line, print out the "A: ". Then in a loop, print out a "* ". The number of iterations through the loop will be the number of A grades. Then after the loop do a System.out.println().
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what exactly am i looping? do i need to write code that loops 'temp' into the number of stars?
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you've got two loops in the latest code snippet. One is a while loop and one is a for loop. It's common for "newbies" (which all of us were at some point) to toss extra loops at a problem. I'd say you just need one loop here, probably the for loop. In many cases it will be good to check the hasNext() method, but in your case you have an explicit end-of-file marker, i.e., -999, so you don't have to worry so much about where the file runs out of data.

Try to go through your program step by step as the computer would. You'll see that you only run the while loop one time, and all the work is actually done in the for-loop. You'll also see something like this: \

Enter while loop
Read first number 70.5
Enter for loop
Store 70.5 in array at position 0
Read second number 49.5
Convert number to a letter grade

When you work through it slowly like that, it's easy to spot the oops! You never converted 70.5 to a letter grade. Back to the drawing board! That's really all programing is: breaking down a problem into logical steps.
 
kate caarlsgard
Greenhorn
Posts: 20
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic