# Comparing and Adding Values in Arrays

Page 1 of 1

## 8 Replies - 3607 Views - Last Post: 08 April 2011 - 06:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=226729&amp;s=cf59a8b66d591fb91d36ac93beeab3f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TheGreatOne2k

Reputation: 0
• Posts: 13
• Joined: 07-April 11

# Comparing and Adding Values in Arrays

Posted 08 April 2011 - 04:41 PM

Hello everybody! I want to compare values in arrays and if they are similar, I want to multiple them by a number without using hundreds of if and else statements. My code is listed below if you all have any ideas.

```import java.util.*;
public class NumberGames
{
public static void sortArrays(int[]x, int i) //Method used to help sort the array.
{
int temp = x[i];
int j = i - 1;
while (j >= 0 && temp < x[j])
{
x[j + 1] = x[j];
j--;
}
x[j + 1] = temp;
}

public static void sortInserting(int[]x, int size) //Method used to insert array values.
{
for (int i = 1; i < size; i++)
{
sortArrays (x, i);
}
}

//My problem is here.  If the values in the array are similar, multiply that number by a certain number.  I have seven different possibilities.  I have comparing
//two values, but I also have to do, three values, four values, and up to seven values.
//I could use a bunch of if else statements, but my code would be longer than the eye can see.  LOL!

public static double addThemUp(int[]numbers) //Method used to calculate values.
{
double total = 0;
while (numbers[0] == numbers[1])
{
total = numbers[0] * 0.1;
System.out.println("Total is " + total);
}
System.out.println();
}

public static void main (String[]args)
{
int[]randomNumbers = new int [7]; //Declare seven numbers array.
for (int i = 0; i < 7; i++)
{
randomNumbers[i] = (int)(7 * Math.random() + 1); //Prints random integers from 1 to 20.
}

sortInserting(randomNumbers, 7); //Method that calls sortInserting method.

for (int i = 0; i < 7; i++)
{
System.out.print(randomNumbers[i] + " "); //Randomly printed numbers.
}
System.out.println();

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Comparing and Adding Values in Arrays

### #2 CasiOo

• D.I.C Lover

Reputation: 1478
• Posts: 3,301
• Joined: 05-April 11

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 04:57 PM

I don't quite understand what you are trying to do. What problem does this solve? You have two numbers, and if one of them is smaller than the other, then you multiply the number with 0.1 and add it to the total varible ?

First of all, you can use /* */ to comment multiple lines .

Second, this will go on in infinity
```    while (numbers[0] == numbers[1])
{
total = numbers[0] * 0.1;
System.out.println("Total is " + total);
}
System.out.println();

```

This post has been edited by CasiOo: 08 April 2011 - 05:02 PM

### #3 TheGreatOne2k

Reputation: 0
• Posts: 13
• Joined: 07-April 11

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:10 PM

Sorry if my question was misunderstood. I declared and instiante a random array, (int)(7 * Math.random() + 1). It will print out 7 values. If 2 of the values are similar in the array. I want to multiple that array by .01. If 3 of the values are similar in the array, I want to multiple them by 0.5. I want to do the same thing if the four of the values are similar and more.

This is what I have so far.
```     public static double addThemUp(int[]numbers) //Method used to calculate values.
{
double total = 0;
for (int i = 0; i < 7; i++)
{
for(int j = 0; j < i; j++)//Loop used to bar exact numbers from being used.
{
if(numbers[i] == numbers[j])
{
total = numbers[i] * .01;
System.out.println("Total " + total);
i--;
}
}
}
}

```

### #4 TheGreatOne2k

Reputation: 0
• Posts: 13
• Joined: 07-April 11

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:41 PM

Sorry if my question was misunderstood. I declared and instiante a random array. It will print out 7 values. If 2 of the values are similar in the array. I want to multiple that array by .01. If 3 of the values are similar in the array, I want to multiple them by 0.5. I want to do the same thing if the four of the values are similar and more.

This is what I have so far.

``` public static double addThemUp(int[]numbers) //Method used to calculate values.
{
double total = 0;
for (int i = 0; i < 7; i++)
{
for(int j = 0; j < i; j++)//Loop used to bar exact numbers from being used.
{
if(numbers[i] == numbers[j])
{
total = numbers[i] * .01;
System.out.println("Total " + total);
i--;
}
}
}
}

```

This post has been edited by TheGreatOne2k: 08 April 2011 - 05:42 PM

### #5 japanir

• jaVanir

Reputation: 1012
• Posts: 3,025
• Joined: 20-August 09

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 05:58 PM

Use nested loops here. for each value in the array, count how many times it repeats in the array.
have a variable counter to count it.
after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.

### #6 TheGreatOne2k

Reputation: 0
• Posts: 13
• Joined: 07-April 11

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:04 PM

japanir, on 08 April 2011 - 05:58 PM, said:

Use nested loops here. for each value in the array, count how many times it repeats in the array.
have a variable counter to count it.
after going over the array use if statements to check the value of that counter. loop the array again and multiply the values if you have to.

Thank you. I will give it a try.

### #7 CasiOo

• D.I.C Lover

Reputation: 1478
• Posts: 3,301
• Joined: 05-April 11

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:06 PM

Something like this? What is done is you make an array which goes from 0 to your biggest numbers. Lets say you have a number with the value 5, then you go to your array index 5 and count it up with 1. This you do for each of the numbers in your numbers array.

``` int total = 0;
int countOfNumbers[] = new int[Maximum number];

for ( int i=0; i<numbers.length; i++ )
countOfNumbers[ numbers[i] ]++;

for ( int i=0; i<countOfNumbers.length; i++) {
if ( countOfNumbers[i] == 2
total += i * 0.01;
else if ( countOfNumbers[i] > 2 )
total += i * 0.03;

}

```

The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

This post has been edited by CasiOo: 08 April 2011 - 06:10 PM

### #8 TheGreatOne2k

Reputation: 0
• Posts: 13
• Joined: 07-April 11

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:13 PM

CasiOo, on 08 April 2011 - 06:06 PM, said:

Something like this? What is done is you make an array which goes from 0 to your biggest numbers. Lets say you have a number with the value 5, then you go to your array index 5 and count it up with 1. This you do for each of the numbers in your numbers array.

``` int total = 0;
int countOfNumbers[] = new int[Maximum number];

for ( int i=0; i<numbers.length; i++ )
countOfNumbers[ numbers[i] ]++;

for ( int i=0; i<countOfNumbers.length; i++) {
if ( countOfNumbers[i] == 2
total += i * 0.01;
else if ( countOfNumbers[i] > 2 )
total += i * 0.03;

}

```

The problem is if you are working with very big numbers, as your helping array with get to be pretty big!

Thank you so much. Arrays and I do not get along. Let me see if I can implement it. Thanks again.

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Comparing and Adding Values in Arrays

Posted 08 April 2011 - 06:20 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }