Hello. I am currently a high-school student. As I am going to apply for Computer Science at my local university after the summer, I have bought the book used in the beginners class for programming. Having solved all the tasks in the first two chapters of the book with relative ease, I found the third chapter to be far more difficult. Here is the task I am struggling with (translated from Norwegian):

Code :

Create a program that allows the user to write in a sequence of integers from 1-9 from the keyboard, and writes out a report about how many times the integers 1-9 occurred. Allow the user to end the sequence with a negative number. For example, if the user writes
8
0
2
8
8
9
5
9
-3
the program should write the following:
0 occurs 1 time.
2 occurs 1 time.
5 occurs 1 time.
8 occurs 3 times.
9 occurs 2 times
Hint: create a table for ten integers that can save the number of times a number has occurred (int[] times = new int[10];)

Can anybody point me in the right direction here?

Please note that I have a very basic knowledge of Java. I have only solved very basic problems, the most advanced of which include creating a calculator for the quadratic formula, a program that allows the user to enter how many numbers he wants to find the average of etc. Thus, I need to be fed this information with a tea-spoon.

You're printing the toString() representation of the int array and not any of the items held by the array. Consider using array indices when printing your array items: myArray[i]. It's the [i] part that you're forgetting.

January 27th, 2013, 03:07 AM

EatMyBible

Re: Saving information from arrays. I think.

Thanks to your post, I now see the logical flaw in my program. It currently looks like this:

I see why it runs like this, seeing as the input itself is never used in the output. I am not sure how to proceed here. Can anyone point me in the right direction?

January 27th, 2013, 04:10 AM

andreas90

Re: Saving information from arrays. I think.

Hello EatMyBible!

Quote:

Originally Posted by EatMyBible

I see why it runs like this, seeing as the input itself is never used in the output. I am not sure how to proceed here. Can anyone point me in the right direction?

You are right. You need to somehow use the input to make your calculations.
Since the input is in [0,10) and your array has length 10, you can use input as the index of the array and add one in that index every time it is entered. Then for example array[0] keeps the value of how many times occured 0, array[1] how many times occured 1, etc.