# Saving information from arrays. I think.

• January 26th, 2013, 01:42 PM
EatMyBible
Saving information from arrays. I think.
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.

EDIT: Just tried this:

Code :

```import java.util.*; public class Kap3Oppg1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] ganger = new int[10]; int input; do { System.out.println("Tast inn neste tall: "); input = in.nextInt(); for (int i=0;i<ganger.length;i++){ System.out.println("Tallet "+i+" forekom "+ganger+" ganger."); } } while (input >= 0);   }   }```

I am getting an error due to i<input.length. i<input is just weird. Am I even walking in the right direction?
• January 26th, 2013, 02:37 PM
Norm
Re: Saving information from arrays. I think.
Quote:

I am getting an error
Please copy the full text of the error message and paste it here.
• January 26th, 2013, 02:47 PM
EatMyBible
Re: Saving information from arrays. I think.
I changed it from i<input.length to i<ganger.length. I do not get an error anymore. This is how it runs, regardless of which number I enter:

Code :

```Tast inn neste tall: 3 Tallet 0 forekom [I@e5b723 ganger. Tallet 1 forekom [I@e5b723 ganger. Tallet 2 forekom [I@e5b723 ganger. Tallet 3 forekom [I@e5b723 ganger. Tallet 4 forekom [I@e5b723 ganger. Tallet 5 forekom [I@e5b723 ganger. Tallet 6 forekom [I@e5b723 ganger. Tallet 7 forekom [I@e5b723 ganger. Tallet 8 forekom [I@e5b723 ganger. Tallet 9 forekom [I@e5b723 ganger.```
• January 26th, 2013, 07:14 PM
curmudgeon
Re: Saving information from arrays. I think.
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:

Code :

```import java.util.*; public class Kap3Oppg1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] ganger = new int[10]; int input; do { System.out.println("Tast inn neste tall: "); input = in.nextInt(); for (int i=0;i<ganger.length;i++){ System.out.println("Tallet "+i+" forekom "+ganger[i]+" ganger."); } } while (input > 0);   }   }```

Which naturally makes it run like this:

Code :

```Tast inn neste tall: 3 Tallet 0 forekom 0 ganger. Tallet 1 forekom 0 ganger. Tallet 2 forekom 0 ganger. Tallet 3 forekom 0 ganger. Tallet 4 forekom 0 ganger. Tallet 5 forekom 0 ganger. Tallet 6 forekom 0 ganger. Tallet 7 forekom 0 ganger. Tallet 8 forekom 0 ganger. Tallet 9 forekom 0 ganger.```

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.

Hope that makes sense.
• February 4th, 2013, 01:48 AM
EatMyBible
Re: Saving information from arrays. I think.
This is how I ended up solving the problem:

Code :

```import java.util.*; public class Kap3Oppg1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int ganger[] = new int[10]; int input = 0; while (true) { System.out.println("Tast inn neste tall: "); input = in.nextInt(); if (input < 0 || input > 9) break; ++ganger[input]; } for (int i=0;i<ganger.length;i++){ if (ganger[i] == 0) System.out.print(""); else System.out.println("Tallet "+i+" forekom "+ganger[i]+" ganger."); }     }   }```

Thanks for all the help.