Results 1 to 20 of 136
Thread: Help with arrays
 03062012, 07:41 PM #1Senior 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
 03062012, 07:51 PM #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?
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 03062012, 08:15 PM #3Senior 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
 03062012, 08:19 PM #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.
 03062012, 08:20 PM #5
Re: Help with arrays
how to get the program to give me the proper output that I want
 03062012, 08:37 PM #6Senior 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[6]; 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[0]+x[1]+x[2]+x[3]+x[4]+x[5]); System.out.print(sumOfNums); if(sumOfNums%3 != 0) { greatestToLeast(x); } else{}//895396998653 ==40 need to take away 2 numbers } public static void greatestToLeast(int[] x) { Arrays.sort(x); int sorted[] = new int[6]; sorted[0] = x[5]; sorted[1] = x[4]; sorted[2] = x[3]; sorted[3] = x[2]; sorted[4] = x[1]; sorted[5] = x[0]; 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++) { } } } }
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
 03062012, 08:44 PM #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.
 03062012, 08:48 PM #8Senior 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
 03062012, 10:05 PM #9Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
Re: Help with arrays
can someone help me with this algorithm?
 03062012, 10:10 PM #10
Re: Help with arrays
Can you explain the problem and give some examples of what needs to be done?
 03062012, 10:22 PM #11Senior 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.
 03062012, 10:30 PM #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
 03062012, 10:44 PM #13Senior 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
 03062012, 10:51 PM #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
 03062012, 10:54 PM #15Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
Re: Help with arrays
can you give me some sort of example please?
 03062012, 10:55 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,017
 Blog Entries
 7
 Rep Power
 23
Re: Help with arrays
I have the stamina of a seal; I lie on the beach instead of running on it.
 03062012, 10:57 PM #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.
 03062012, 10:59 PM #18Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
 03062012, 11:00 PM #19Senior Member
 Join Date
 Mar 2011
 Posts
 171
 Rep Power
 0
 03062012, 11:03 PM #20
Similar Threads

Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02172012, 01:49 PM 
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06142011, 01:28 PM 
help with arrays
By droidus in forum New To JavaReplies: 20Last Post: 04192011, 06:29 AM 
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04192010, 10:32 AM 
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 02:40 AM
Bookmarks