Results 1 to 5 of 5
- 12-05-2009, 01:51 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
Error if array contains duplicate integers
I posted earlier about an array having random integers but one integer cannot appear twice. I got some feedback and was finally able to figure it out, but now I'm stuck at another place.
Previous topic: An Array of different integer values
Basically, the whole concept is to have a computer generate a random number of digits between 2 and 10, with each digit in the number NOT appearing twice.
Then, the user must enter a number from his keyboard to guess the number, knowing the number of digits in the randomly generated number. I'm trying to accomplish this part but it's not happening.
What I did was ask for a number from the user, then pass it into a .hasNext() and then verify the input to make sure it contains numbers, is of the right size, BUT I'm stuck at finding out whether the number contains any duplicate values.
Here is my code
I first see that the digits in the number entered from the keyboard is of the correct value. If it is, then make a userArray of this number. Then, check each digit in the array against the other in the function "checkSimilarDigits" and return a boolean.Java Code:// // File: Master.java Compiler: Scite // import java.util.Scanner; public class Master { final static int MAX = 10 - 1; // the maximum digits in a randomly generated number final static int MIN = 2; // the minimum digits in a randomly generated number final static char YES = 'Y'; // to continue or keep going final static char NO = 'N'; // to stop public static void main (String[] args) { int size = randomSize(); // call a function to randomly generate an integer based on constant restrictions int[] computerArray = createArray(size); printArray(computerArray); // call functions to create an array and print it System.out.println("The number consists of " + size + " values.\n"); System.out.println("What's your guess?"); System.out.print("Answer: "); int[] userArray = verifyAnswer(computerArray); printArray(userArray); } private static int[] verifyAnswer(int[] computerArray) { Scanner input = new Scanner(System.in); // start keyboard Scanner int guess = 0; // initialize the guess to 0 int digits = 0; // initialize the number of digits in the guess to zero int[] userArray = new int[computerArray.length]; // make an array equal to the randomly generated computer array while (input.hasNext()) // create a loop to keep asking for a value until the right value has been entered { if (input.hasNextInt()) // if the user entered an integer { guess = input.nextInt(); // set guess to this integer value digits = countDigits(guess); // count the number of digits in this guess to see if it equals the array if (digits > computerArray.length || digits < computerArray.length) // if it's less or more { System.out.println("\nThe value you entered is not of the correct range"); // show the error System.out.print("Enter a guess: "); // let them try again } else // if it is equal to the array { userArray = createUserArray(digits, guess); // create an array of the value entered boolean check = checkSimilarDigits(userArray); // check if this array has any duplicate values if (check == true) // if it does { System.out.println("The value you entered has duplicate digits!"); // show the error System.out.println("Please use a digit only once!"); // tell them how to fix it System.out.print("Enter a guess: "); // let them try again } } } else { input.next(); // if user input is anything other than an integer System.out.println("\nThat is not a valid value!"); // show the error // tell them what the correct range should be System.out.println("Please guess an INTEGER value that consists of " + computerArray.length + " digits"); System.out.print("Try again: "); // let them try again } } return userArray; } private static int countDigits(int number) { int count = 0; // initilaize counter to zero if (number < 0) // check to see if the number is positive number *= -1; // if not, turn it into a positive value while (number > 0) // begin loop to start counting digits { number /= 10; // keep chopping off a digit count++; } return count; } private static int randomSize() // function to generate a random integer { return MIN + (int)(Math.random() * MAX); // return an integer between those constant values } // function end private static int[] createArray(int size) // function to create an array based on a random size generated in the "randomSize" function { int[] array = new int[size]; // create the new array of the size of that integer variable int count = 0; // initialize a timer for (int i = 0; i < array.length; i++) // start adding values to the array { if(i == 0) // for the first element in the array { array[i] = randomInteger(); // call a function to assign this a random integer count++; // increase the counter by 1 } else // for all other elemebts in the array { int randomvalue = randomInteger(); // call a function to generate a random integer and assign it to this variable boolean doesexist = search(array, randomvalue, count); // do a check to see if the variable equals any other values in the array while (doesexist != true) // while the variable value does exist { randomvalue = randomInteger(); // keep generating random values and assigning it to the variable doesexist = search(array, randomvalue, count); // until you find one that hasn't already been assigned } array[i] = randomvalue; // assign the next element in the array that randomly generated value count++; // and increase the counter by 1; } } return array; // return this array } // function end private static boolean search(int array[], int value, int count) // function to search for similar integer values { for (int i = 0; i < count; i++) // start a loop that goes uptil the array size that has already been assigned values { if (value == array[i]) // if the randomly generated variable equals any array element return false; // return a false } return true; // otherwise return a true } // funtion end private static int randomInteger() // function to get a random integer { return (int)(Math.random() * 10); // return a random integer from 0 to 9 } // function end private static int[] createUserArray(int size, int number) // function to create an array based on a random size generated in the "randomSize" function { int[] array = new int[size]; // create the new array of the size of that integer variable for (int i = array.length - 1; i >= 0; i--) // start adding values to the array { array[i] = (number % 10); number /= 10; } return array; // return this array } // function end private static boolean checkSimilarDigits(int[] array) { for (int i = 0; i < array.length; i++) { for (int j = 1; j < array.length - 1; j++) { if (array[i] == array[j]) return true; } } return false; } private static void printArray(int[] array) // function to print an integer array { for (int i = 0; i < array.length; i++) // start loop to start printing { System.out.print(array[i]); // print all values one after the other } System.out.println(); } // function end }
If it is true, show an error and go back into the loop, otherwise return.
However, it doesn't work. The boolean keeps returning whatever I assign it and doesn't do it's job. So no matter which number I chooce, with similar or non-similar digits, I still get the error.
Can anyone please tell me what I'm doing wrong here?
- 12-05-2009, 02:07 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Java Code:private static boolean checkSimilarDigits(int[] array) { for (int i = 0; i < array.length; i++) { for (int j = 1; j < array.length - 1; j++) { if (array[i] == array[j]) return true; } } return false; }
Consider what happens the second time around the outer loop when i is one. j will also be one initially so "array[i] == array[j]" will be true and this method will (almost always) return true. Is this what you want?
- 12-05-2009, 07:50 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
I see what you're saying! I finally see it! LOL
No, I want to find duplicate values within the array. How would I do that? I know that i and 1 will always equal j at one which is why this is happening.
How can I do this without that happening?
- 12-05-2009, 08:14 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
The easiest way would be to check i and j. If they are equal don't bother checking array[i] and array[j].
- 12-05-2009, 08:58 AM #5gcampton Guest
Similar Threads
-
Sorting 3 Integers Using If Else
By MSteinman in forum New To JavaReplies: 12Last Post: 02-19-2010, 12:52 PM -
DOnt know if 1st post if did, I am VERY sorry for duplicate post. I have error messg
By afisher300 in forum New To JavaReplies: 3Last Post: 05-04-2009, 03:15 AM -
Counting Duplicate Variables in an Array
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-24-2008, 07:33 PM -
Help with Code! Display Array of Strings and Integers - Sorted
By luvjoynt in forum New To JavaReplies: 7Last Post: 04-28-2008, 04:28 AM -
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks