aspose file tools*
The moose likes Beginning Java and the fly likes Running...tally...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running...tally...?" Watch "Running...tally...?" New topic
Author

Running...tally...?

Sebastian Smith
Greenhorn

Joined: Oct 29, 2005
Posts: 6
Hi,

I'm very very new to Java so I'll try my best to explain my dilemma.

Basically, as an exercise, we must create a program that asks the user for input of scores and then outputs the scores as grades (ie enter 80, it says you have a distinction, enter 50 you have a pass, etc). The user can type as many grades as they want, and then can stop entering the grades by entering a negative number.

Now, I can do that part with a while loop and if, else if, loops, but i need for Java to keep count of the different scores, then process them into grades. As of now, its just taking the last score entered and then processing that into a grade.

Thanks for any help
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sebastian Smith:
... i need for Java to keep count of the different scores, then process them into grades. As of now, its just taking the last score entered and then processing that into a grade...

Welcome to JavaRanch!

I'm a little unclear on what you're after. If all you want to do is "keep count" (i.e., a running tally) of scores, then an int counter should do the trick, calling counter++ each time you process input.

On the other hand, maybe you're looking for a way to store each score that's input. The easiest way to do this would be an int[] used in conjunction with the counter described above. Then -- supposing you call the array "scores" -- you would have scores[counter] = input;

The problem with an array is that you need to set the size when you create it, so you'll have an upper bound on the number of scores it can hold. One way to address this limitation is with an ArrayList (in java.util), which is basically an automatically re-sizable array. However, unlike a simple array, an ArrayList can only hold objects -- not primitives. So you would need to wrap your int scores as Integers to put them into the ArrayList, and then cast them back down to type Integer when you pull them out (because within the ArrayList they're actually stored as type Object). I won't go into too much detail on this unless you decide to go this route and have questions...


"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
Sebastian Smith
Greenhorn

Joined: Oct 29, 2005
Posts: 6
Cheers for the reply.

It sounds like I may need that second method, the array.

I'll rephrase myself ....Basically, I need for java to read a list of scores that the user types in. When the user is done, java displays the output as a list of grades (converting the scores to grades, so, e.g. the output would be 4 passes, 4 fails, etc)

However, we haven't touched upon arrays (we've mostly been working on loops)yet in the course, so is there an alternative method you can think of?


Thanks again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sebastian Smith:
...so, e.g. the output would be 4 passes, 4 fails, etc...

If you just need to tally the number of pass, fails, etc. without keeping track of the associated scores, then you could just use separate int counters for each of these...

int passCount;
int failCount;

Then for each input, determine the category (pass, fail) and increment the appropriate counter.
Sebastian Smith
Greenhorn

Joined: Oct 29, 2005
Posts: 6
Just to make sure, would these inputs go before my if,else if, loop, or after it?

Or would it not make a difference?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sebastian Smith:
Just to make sure, would these inputs go before my if,else if, loop, or after it? Or would it not make a difference?

The best way to answer that is for you to experiment writing the code.

I would just suggest declaring the counter variables outside of the loops, so that they will be in scope from anywhere within the loops. The rest really depends on how your loops are structured.
Sebastian Smith
Greenhorn

Joined: Oct 29, 2005
Posts: 6
Sorry, I'm still at a loss...heres my code.

Its just taking the first score entered and converting that. anything after that is ignored.



import java.util.*;

public class test{

public static void main(String[]args)
{


int score, distinctCount, passCount, failCount;
distinctCount=0;
passCount=0;
failCount=0;

System.out.println("Enter the exam scores you wish to find the grades for.");
System.out.println("When you have finished, enter a negative value to end.");
Scanner keyboard = new Scanner(System.in);
score = keyboard.nextInt();


if (score >= 80)
distinctCount++;
else if (score >=50)
passCount ++;
else if (score >=0)
failCount ++;

while (score >=0)
score = keyboard.nextInt();

System.out.println("Number of distinctions equals " + distinctCount);
System.out.println("Number of passes equals " + passCount);
System.out.println("Number of fails is " + failCount);
}
}
[ October 30, 2005: Message edited by: Sebastian Smith ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Its just taking the first score entered and converting that. anything after that is ignored.


Unfortunately, that is *exactly* what your code is doing. You are reading in one value, process it, and then go into a while loop that reads the rest of the values, and do nothing else.

You have to loop the reading and processing of all the values.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sebastian Smith
Greenhorn

Joined: Oct 29, 2005
Posts: 6
But the while loop being where it is, after the processing, shouldn't it be reading and then processing for the other values as well?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Originally posted by Sebastian Smith:
But the while loop being where it is, after the processing, shouldn't it be reading and then processing for the other values as well?


The program will only do what you tell it to, your "while" loop, no matter where it is, is still a while loop that reads in values, and nothing else.

Henry
Sebastian Smith
Greenhorn

Joined: Oct 29, 2005
Posts: 6
Right, I see what you mean now. I was getting confused because it was working for the first value. So every other subsequent value (via the while loop) is only being read, not processed. Thanks.

EDIT : I got it working. You've made my day...cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running...tally...?