1. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Help with arrays

I have an array of length 6 and I would like to search through it and remove one of the integers that is in the array. It is an array made up of 6 digits, i.e. 123456.

So, I would also like to possibly change it to a string and have the ability to change that string back into an array.

Ideas anyone? do you think it might be easier to deal with using an arraylist? I'm open for any idea  Reply With Quote

2. ## Re: Help with arrays

Which part of this are you having trouble with? You've outlined several steps here, which one are you stuck on? What do you have so far?  Reply With Quote

3. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

So I am not sure really. I created a random number generator so that I can create a 6 digit number where I can add up all the numbers that are in it. The best way I figured to do that is using an array. Now what I am trying to do is figure out how to search that array for a certain number and if it is there, remove it.

I am trying to create a simple math game, so eventually I will have to figure out how to put the input into strings so the user can input a 6 digit number and then I will change that to an array of length 6. But for now I am just trying to figure out how to get the program to give me the proper output that I want. I will worry about the input and strings and such later  Reply With Quote

4. ## Re: Help with arrays

A question: what do you mean by a digit?
'1' is a character digit. "2" is a String
3 is an int value that takes 2 binary bits to hold its value.  Reply With Quote

5. ## Re: Help with arrays

how to get the program to give me the proper output that I want
Post the code and its output and explain what the problem is.  Reply With Quote

6. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

Java Code:
```import java.util.*;
public class Hardcode {

public static void main(String[] args)
{
randomNumber();
}

//start of hard coding
private static int sumOfNums = 0;
public static void randomNumber()
{
int x[] = new int;
int index = 0;
Random generator = new Random();
for(int i = 0; i<6; i++)
{
int randomInt = generator.nextInt(10);
System.out.print(x[i] = randomInt);
index++;
}
checkDivisibility(x);
}

public static void checkDivisibility(int[] x)
{
System.out.println();
sumOfNums = (x+x+x+x+x+x);
System.out.print(sumOfNums);
if(sumOfNums%3 != 0)
{
greatestToLeast(x);
}
else{}//895396-998653 ==40 need to take away 2 numbers
}

public static void greatestToLeast(int[] x)
{
Arrays.sort(x);
int sorted[] = new int;
sorted = x;
sorted = x;
sorted = x;
sorted = x;
sorted = x;
sorted = x;
System.out.println();
for(int i = 0; i < sorted.length; i++)
{
System.out.print(sorted[i]);
}
removeDigit(sorted);
}

public static int removeDigit(int[] x)
{
if(sumOfNums%3 == 1)
{
for(int i = 0; i < 6; i++)
{
}
}
}
}```
So what I need help with is the removeDigit method.

Lets say I get a random number like 897261. The program then checks if it is divisible by 3. If not it sorts it from least to greatest, and then moves it into a new array that makes it greatest to least. So now the output after the greatest to least method for this number would be 987621. This number is equal to 33, so it is divisible by 3, but if it wasn't I would want to remove the smallest possible number to make it divisible by 3. so lets say the number was the one that i just noted in my code...998653. In this case it equals 40, with a remainder of 1. so you either want to remove a 1, 4, 7 etc... but in this case we would have to remove 2 of those numbers there the 8 and 5 will give you the largest possible number from those that is divisible by 3. 9963. Granted I am sure there are other possibilities, but I want the biggest number that can be made.
So for a simpler example, 987622. That is equal to 34. You would think just take out the two 2's, but in fact you want to remove the 7 because 98622>9876.

That is as best as I can explain it. Not as easy as said as done lol  Reply With Quote

7. ## Re: Help with arrays

Do you have a problem with coding your algorithm in java
or are you having problems designing an algorithm for your program?

If the problem is with java coding, please explain.  Reply With Quote

8. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

i guess more coming up with the algorithm to execute it that i need the help with  Reply With Quote

9. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

can someone help me with this algorithm?  Reply With Quote

10. ## Re: Help with arrays

Can you explain the problem and give some examples of what needs to be done?  Reply With Quote

11. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

I am having trouble figuring out how to execute what I want to be done. as I said in the previous posts take a number, see if it is divisible by 3, if it is then done, if not, sort it from greatest to least, and remove 1 or 2 of the digits to make it divisible by 3, but it be the highest possible number prodoced.

Lets say I get a random number like 897261. The program then checks if it is divisible by 3. If not it sorts it from least to greatest, and then moves it into a new array that makes it greatest to least. So now the output after the greatest to least method for this number would be 987621. This number is equal to 33, so it is divisible by 3, but if it wasn't I would want to remove the smallest possible number to make it divisible by 3. so lets say the number was the one that i just noted in my code...998653. In this case it equals 40, with a remainder of 1. so you either want to remove a 1, 4, 7 etc... but in this case we would have to remove 2 of those numbers there the 8 and 5 will give you the largest possible number from those that is divisible by 3. 9963. Granted I am sure there are other possibilities, but I want the biggest number that can be made.
So for a simpler example, 987622. That is equal to 34. You would think just take out the two 2's, but in fact you want to remove the 7 because 98622>9876.  Reply With Quote

12. ## Re: Help with arrays

Is this the problem?
take a random 6 digit number,
see if it is divisible by 3,
if it is then done,
if not, sort the digits from greatest to least: For example: 234567 goes to 765432

and remove 1 or 2 of the digits to make it divisible by 3, <<<<<<< THIS LOOKS like the hard part

but it be the highest possible number produced <<<< What does this mean??? Show an example
Could you show an example?  Reply With Quote

13. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

That is the hard part! hahaha

So, a number like 234567 right? not divisible by 3 as the digits in it add up to 27.
So now we have the number sorted. 765432.
In order for this number to become divisible by 3 we will have to remove some digits. There are numerous combinations of digits that we can remove. However, I want to remove only the number(s) so that while still ordered it is the highest number possible.

i.e. for this example I would remove the 3 as opposed to the 6, even though they both give us a number divisible by 3. So, I want 76542 because that is > 75432.

Is it starting to make some sense now? hahaa  Reply With Quote

14. ## Re: Help with arrays

use a loop and build a number by dropping out one of the 6 digits each time around the loop  Reply With Quote

15. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays

can you give me some sort of example please?  Reply With Quote

16. ## Re: Help with arrays Originally Posted by adjit In order for this number to become divisible by 3 we will have to remove some digits. There are numerous combinations of digits that we can remove. However, I want to remove only the number(s) so that while still ordered it is the highest number possible.

i.e. for this example I would remove the 3 as opposed to the 6, even though they both give us a number divisible by 3. So, I want 76542 because that is > 75432.

Is it starting to make some sense now? hahaa
Not really; if a number can not be divided by three and one of the digits is a three, removing it doesn't make the resulting number dividable by three; there's nothing laughable about it.

kind regards,

Jos  Reply With Quote

17. ## Re: Help with arrays

Break up the number into the individual digits -> into an array??
Then write a loop to build a number using 5 of the 6 digits leaving one out each time.  Reply With Quote

18. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays Originally Posted by JosAH Not really; if a number can not be divided by three and one of the digits is a three, removing it doesn't make the resulting number dividable by three; there's nothing laughable about it.

kind regards,

Jos
I wasn't saying take out the 3 because of the fact that it is a 3. I was saying take out the 3 because it will give us the desired number that is divisible by 3.

It could be any number so long as the final product is divisible by 3  Reply With Quote

19. Senior Member Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Help with arrays Originally Posted by Norm Break up the number into the individual digits -> into an array??
Then write a loop to build a number using 5 of the 6 digits leaving one out each time.
Don't really know what you mean...  Reply With Quote

20. ## Re: Help with arrays

which part don't you understand?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•