File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes counters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "counters" Watch "counters" New topic
Author

counters

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Hi guys. Tell me this, if you wanted to count the number of A's, B's, C's, D's and so on in a program after the program has calculated the grades, how would you do this? I know it needs some type of counter. My code looks something like this...
___________________________________________________________________
if ((next >= 90) && (next <= 100 )) next = 'A';
counterA++;
System.out.println("Number of A's = " + counterA)
__________________________________________________________________

And I have declared int CounterA = 0;
and so forth.
What else do I need to do. Right now my program only shows one of each grade, A's - 1, B's- 1, and so on.
What am I doing wrong?
Thanks for your help.


) ) ) )
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think you'll want to add some braces to your if statement so that counterA only gets incremented if the condition is true. Otherwise, this part looks okay, so I think the answer to your question really depends on what else surrounds this code.



(I'm curious about "next." It appears to be a score at first -- probably an int or double. But then it gets set to the value of a char...?)
[ October 14, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Assuming the grades go like this
90 - 100 = A
80 - 89 = B
70 - 79 = C
60 - 69 = D
< 60 = F

you could set up an array
int[] counter = new int[5];

Inside a for loop (looping the scores array) your if statement can be like
this (no need for both >= && <=)

if(score >= 90) counter[0]++;
else if(score >= 80) counter[1]++;
etc

then you loop through counter[] to get the results
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Rose Evans
Let me share about my opinion on it
First of all, you have declared a variable named "CounterA "
please remember that Java is Case-sensitive

Sorry that I do not really understand your question
There are several method that you can use.
if ---- else if or switch condition that you could use.
And you did not learn what is array, you can just use some simple vairables which can help you to store the number of that grade. Just what you did.


Francis Siu
SCJP, MCDBA
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thanks guys for all your help!! I'll see if I can do something different with my code..

Rose
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Shoot guys, I still must not know what I am doing. It has to be an easy counter that I need, but I can't seem to figure it out.
______________________________________________________________________

if ((next >= 90) && (next <= 100 )) next = 'A';
counterA++;
System.out.println("Number of A's = " + counterA);

if ((next >= 80) && (next <= 89)) next = 'B';
counterB++;
System.out.println("Number of B's = "+ counterB);
________________________________________________________________________

What in the world am I doing wrong.
After someone inputs the grades, which I call next, and after the program runs, the program is supposed to count the number of A's, B's etc. I have set a variable counterA = 0.
Each time I run the program, it just outputs the number 1 for each A, B, C and so on. I know there has to be a simple counter to do this.
Any ideas? Thanks so much for you help. I think I am brain dead.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

I assume there's some kind of loop for testing all the grades? Also, look at marc weber's post - you still need those curly braces... here's your code with some annotations...



if you change your code to this:


I might suggest slowing down a little in your code. don't try and do it all at once - you're garunteed to freak out then when it doesn't work (and it never works the first time - ask anybody here). I might suggest breaking down your problem into smaller pieces, and make sure they work before you start the next.

So, assumeing you have calculated all the grades somewhere, start off by just testing all of them to see what the letter should be, and print them out... something like

A
D
B
A
C
A
...

and so on. Once THAT works, THEN try and count how many of each, and possibly print it out on every line....

number of A's: 1
number of D's: 1
number of B's: 1
number of A's: 2
number of C's: 1
number of A's: 3
...

and so on.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Fred,
Thank you!! This will really help!!! You seem to be really good at java. Keep up the great work.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: counters