question states for me to have the prog read #'s inputed from the user in the range of 0 to 50 inclusive, count how many times each # was inputed, then print all #'s imputed and how may times they were. this is what I have so far....

import java.util.Scanner;

public class ReverseOrder

{
public static void main (String[] args)
{	
	Scanner scan = new Scanner (System.in);
	double[] numbers = new double[10	];
	double apple
	
	System.out.println ("The size of the array: " + numbers.length);
	
	for (int index = 0; index < numbers.length; index++)
	{
	System.out.print ("Enter a number between 0 and 50: " + (index+1) + ": ");
		apple = scan.nextDouble();
	}

so what ive done here is tell the user that he/she will have 10 entries to enter a number
then what i think i'm doing is setting the imput to the int "apple". next what i'm trying to do:
(certainly not in acceptable coding, but just trying to think/communicate it out)
if (apple = apple) // if the numbers are the same then increase the count
count++
System.out.println (number[index] + count) //print all of the numbers entered and how many times

but theres the problem, logically at least. how do i assign the count to each particular int? and then get it to print out that particular count to the right int? as it stands right now im sure itll just up the count for any random int that happens to be the same. any help much appreciated

Recommended Answers

All 2 Replies

1. I think you want an array of integers rather than doubles.
2. According to the problem definition, I think you need your array to be 51 elements long (in order to include both 0 and 50).
3. You need to initialize the array so that all the numbers are 0.
4. When you get a number input from the user, you can use that number to index into the array and increment the array value at that position.
5. You need some way for the user to indicate that s/he is done entering numbers. For example, you could ask the user to enter -1 when done.
6. After getting all the input from the user, you can iterate over the elements in the array in a loop and print out how many of each number was entered.

The array declaration will take care of the zero initialization for you. We're not in C any more... :)

Agree on all of the other points. John410 - read point #4 carefully. You're making an array, and each cell of the array contains the number of times the index of that cell has appeared in the input.

"apple" should also be an int, and it should have a name that reflects its purpose. "apple"? What does that tell the guy reading the code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.