# What Am I Doing Wrong?(Arrays)

• May 1st, 2013, 02:52 PM
zendorz
What Am I Doing Wrong?(Arrays)
So were suppose to create a program that reads in integers in the range of 0-50, puts them in an array
and prints out how many times each integer is entered. Sorry I forgot to add that its not working properly and am wondering what can I fix?

Code java:

```package HW12;   import java.util.Scanner;   //HW12.java Stephen Wells //CS140-01 //This program reads in integers in the range of 0-50, puts them in an array //and prints out how many times each integer is entered.   public class HW12 { public static void main (String [] args)   { //Explains what the program does System.out.println("This program reads in integers in the range of 0-50, " + "puts them in an array and prints out how many times each integer is entered."); //Build the Scanner object so user can read in information from the keyboard Scanner scan = new Scanner (System.in);   //Declare an array called "occurrences" that will hold 51 integers (0-50) System.out.println("type size of array: "); int number = scan.nextInt(); int[] array=new int[number];   //Prompt the user to enter a random number of integers from 0-50 for(int i=0; i<array.length; i++) { array[i]=scan.nextInt(); } System.out.println("The integers entered are: ");   //Get the integers from the keyboard for(int i=0; i<array.length;i++) { System.out.println(array[i]); } for(int i=0; i<array.length;i++) { System.out.println(i+": "+array[i]); }       //Use a while loop to validate that the user enter an integer between 0 and 50 //the body of the while loop will capture the integers and put them in the array while //counting them while (number < 0 || number > 50) { System.out.println("invalid - please enter a number between 0 and 50"); number = scan.nextInt(); }   //Print the results using a for loop and an if statement4To print out the numbers you then just need to change your last for loop to be: for (int i = 0; i < array.length; i++) { if (array[i] > 0) { System.out.println(i + "The total number of times the integer is entered is: " + array[i]); } }   } }```
• May 1st, 2013, 02:53 PM
Norm
Re: What Am I Doing Wrong?(Arrays)
Did you have any questions about the posted code?

[code=java]