I'm having a problem with this program that I'm making. Here's the purpose of the program:

Write a program that will get number inputs from the user. If the user inputs a negative number, the program will stop accepting inputs from the user. Then, the program will find repeating numbers from the program and determine how many times it appeared.

Here's the sample output:

Enter a number : 5

Enter a number : 5

Enter a number : 7

Enter a number : 2

Enter a number : 7

Enter a number : 3

Numbers Entered:

2 _________ 1

3 _________ 1

5 _________ 3

7 _________ 2

Note: If the number appeared two or more times, it will not duplicate in the output. The numbers are also arranged.

I already figured out first part (if the user inputs a negative number)

Scanner scan = new Scanner(System.in);

ArrayList<Integer> arrayNumbers = new ArrayList<Integer>();

ArrayList<Integer> countArray = new ArrayList<Integer>();

int x,;

int counter=0;

int confirm;

int length=0;

int input;

int z;

int elements=0;

for (x=0;x<=counter;x++)

{

if (counter==x)

{

System.out.print("Enter a number : ");

input = scan.nextInt();

confirm = input;

if (confirm<0)

{

counter--;

arrayNumbers.remove(x);

}

else

{

counter++;

length++;

}

}

}

I am now stuck with the comparing part of the program. I've done a code, but it doesn't seem to work:

int blah,count,z10;

Arrays.sort(finalArray);

for (int i=0;i<finalArray.length;i++)

{

blah = finalArray[i];

count=0;

while(finalArray[i]==blah&&z10<finalArray.length)

{

count++;

i++;

}

System.out.println("Number : "+blah+" Count : "+count);

}

Can someone please help me with the logic or the code? Help will be much appreciated. Thanks :)

P.S. I'm required to do this using Arrays only. No other methods.

you should user a HashMap< int,int > where you store the number and the number of times the user entered it

try something like this (this is a basic idea not the real code):

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

when the user enters the number do something like this:

if (map.containsKey(input)) {
int counter = map.get(input) + 1;
map.put(input, counter)
}

Okay, I read over the question in more detail, there are some things I want you to think about. And since this is obviously a school project, I'll try to give you hints on what you need to do.

First, the negative number part you mention. You're doing a for loop and comparing against a counter that you're increasing with each number added. You add each number to the array and if the last number added was negative, you remove it and reduce the counter that ends the for loop. While this works this is not a good readable code. You should look into using a while loop that checks the input, the pseudocode would be somewhat like this:

int input
array numberArray

read number into input from user
while input >= 0:
add input to numberArray
read number into input from user

This will read the number from the user and if it is not negative it will add to the array and read a number again, then repeating the input read from the user as long as it's not negative.

In the latter part you mention you need help with you'll have a sorted array of numbers and you want to print out how many of each number there is in the array you could do it similar to how you are doing it. With the limitations you have (and my limited java knowledge) I would probably do someting like this pseudocode shows:

sort numberArray
int currentNumber = -1
int counter

for i = 0 while i < array.length do i++:
if numberArray[i] is currentNumber:
counter++
else //if new number
print how many times currentNumber has been encountered (before assigning currentNumber to a new number)
currentNumber = numberArray[i]
counter = 1
print how many times currentNumber has been encountered

This should loop through the whole array, count how many times in a row a number is in the array and when you find a new number, you'll print out the current tally, select the new number and set the counter to 1. The reason for the print after the loop is that when we hit the final number in the array, we wont go into the else branch of the if/else where we had our print statement.

I hope this gives you some idea on how you could do it.

Top