Counting a total and printing value from 2 arrays

Hi Guys!

This is my first post and i am a complete newbie to Java...

I am trying to total and print the number of apples in each basket. But when i print it prints each transaction at each point in the array, causing a double up in my print information. Did that make sense? :) Please see below.

this is my code so far...

int size = 10;

int arrayOneValue;

int arrayTwoValue;

int count;

int maxBasketNumber;

int appleCount;

maxBasketNumber = 0;

for (arrayOneValue=0; arrayOneValue < size; arrayOneValue = arrayOneValue +1)

{

if(BasketNumber[arrayOneValue] > maxBasketNumber)

maxBasketNumber = BasketNumber[arrayOneValue];

}

appleCount = 0;

for (arrayOneValue=0; arrayOneValue < size; arrayOneValue = arrayOneValue +1)

{

count = 0;

for(arrayTwoValue = 0; arrayTwoValue < size; arrayTwoValue++)

{

if(BasketNumber[arrayOneValue] == BasketNumber[arrayTwoValue])

count++;

}

System.out.println("BasketNumber: " + BasketNumber[arrayOneValue] + " Total of apples: " + count);

System.out.println("");

appleCount++;

}

System.out.println("");

This is the output:

BasketNumber: 1 Total of apples: 3 // I would like to print each BasketNumber only once with its value not repeat the same info

BasketNumber: 1 Total of apples: 3

BasketNumber: 3 Total of apples: 3

BasketNumber: 0 Total of apples: 2

BasketNumber: 3 Total of apples: 3

BasketNumber: 2 Total of apples: 1

BasketNumber: 0 Total of apples: 2

BasketNumber: 4 Total of apples: 1

BasketNumber: 1 Total of apples: 3

BasketNumber: 3 Total of apples: 3

Any help would be greatly appreciated!

:)

Re: Counting a total and printing value from 2 arrays

2 small things. First of all, try using the .code=java] ./code] tags (replace the . with a [) so it's easier to read. Also why do you do
Code java:

arrayOneValue = arrayOneValue +1

instead of just arrayOneValue++? makes life so much easier, and you are doing it in your other for loop as well.

Also could you give us a runnable version? makes it easier to look at the problem. For example what is BasketNumber? And how many elements can it store (the size)?

Re: Counting a total and printing value from 2 arrays

Ok i will make those changes :)

import java.util.*;

public class basket

{

public static long number=1234567;

public static int basketNumber[];

public static double apples[];

{

basketNumber=new int[size];

apples=new double[size];

Random rand=new Random(number);

for (int i=0;i<size;i++)

{

basketNumber[i]=rand.nextInt(size/2);

apples[i]=rand.nextInt(10000)/100.0;

}

}

public static void main(String args[])

{

int size=10;

initialiseData(size);

int arrayOneValue;

int arrayTwoValue;

int count;

int maxbasketNumber;

int appleCount;

maxbasketNumber = 0;

for (arrayOneValue=0; arrayOneValue < size; arrayOneValue = arrayOneValue ++)

{

if(basketNumber[arrayOneValue] > maxbasketNumber)

maxbasketNumber = basketNumber[arrayOneValue];

}

appleCount = 0;

for (arrayOneValue=0; arrayOneValue < size; arrayOneValue = arrayOneValue ++)

{

count = 0;

for(arrayTwoValue = 0; arrayTwoValue < size; arrayTwoValue++)

{

if(basketNumber[arrayOneValue] == basketNumber[arrayTwoValue])

count++;

}

System.out.println("Basket Number:: " + basketNumber[arrayOneValue] + " Total of apples: " + count);

System.out.println("");

appleCount++;

}

System.out.println("");

This what output should look like...

Basket Number: 1, Apples: 3

Basket Number: 3, Apples: 3

Basket Number: 0, Apples: 2

Basket Number: 2, Apples: 1

Basket Number: 4, Apples: 1

Thanks for your help :)

Re: Counting a total and printing value from 2 arrays

Please Edit your post and wrap your code with

[code=java]

<YOUR CODE HERE>

[/code]

to get highlighting and preserve formatting.