Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Thread: Saving information from arrays. I think.

1. 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):

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:

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?

2. Re: Saving information from arrays. I think.

I am getting an error
Please copy the full text of the error message and paste it here.

3. 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:

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.

4. 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.

5. The Following User Says Thank You to curmudgeon For This Useful Post:

EatMyBible (January 27th, 2013)

6. 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:

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:

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?

7. Re: Saving information from arrays. I think.

Hello EatMyBible!

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.

8. The Following User Says Thank You to andreas90 For This Useful Post:

EatMyBible (January 27th, 2013)

9. Re: Saving information from arrays. I think.

This is how I ended up solving the problem:

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.