Page 1 of 7 123 ... LastLast
Results 1 to 20 of 136
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

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

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default 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{}//895396-998653 ==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++)
    				{
    				}
    			}
    		}
    }
    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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

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

  8. #8
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

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

  9. #9
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    can someone help me with this algorithm?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Help with arrays

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

  11. #11
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

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

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

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

  13. #13
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default 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

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Help with arrays

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

  15. #15
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    can you give me some sort of example please?

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,770
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with arrays

    Quote Originally Posted by adjit View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

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

  18. #18
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Quote Originally Posted by JosAH View Post
    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

  19. #19
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Quote Originally Posted by Norm View Post
    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...

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

Page 1 of 7 123 ... LastLast

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  3. help with arrays
    By droidus in forum New To Java
    Replies: 20
    Last Post: 04-19-2011, 07:29 AM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Posting Permissions

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