Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Counting a total and printing value from 2 arrays

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!



  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default 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
    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)?

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 09:45 AM
  2. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM