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?