I am currently trying to write a program which asks the user for a three digit number and checks it against a random 3 digit number generate by the computer. The number the user enters must have three unique digits, as the computer number also has three unique digits.
So far I have managed to get the user’s three digit number into an array with one digit in each array position, but I can’t get it to validate properly. It is able to identify two digits being the same, so it would then need to ask the user for a new number that has 3 different digits (i.e. loop back to beginning) until such time as user complies. The System.out.println is just for me to check that it is picking out the double digits. Here is my code so far. This is a method, not the whole program.
public static int[] getUserNumberDebugMode(String compNum){
final int RANDOM_NUMBER_SIZE = 3;
int[]userNumber = new int [RANDOM_NUMBER_SIZE];
String userGuessAsString;
userGuessAsString = JOptionPane.showInputDialog(compNum + "\n Please enter a number.");
int number = Integer.parseInt(userGuessAsString);
// put three digit number individually into array
for(int loop = (userNumber.length - 1); loop>=0; loop--){
userNumber[loop]= number%10;
number = number/10;
} // end userNumber array populating
for (int outerloop = 0; outerloop < userNumber.length; outerloop++){
for (int innerloop = outerloop - 1; innerloop > -1; innerloop --){
while (userNumber[outerloop] == userNumber [innerloop]){
System.out.println("user compare: " + outerloop + " with: " + innerloop);
} // end while loop
}// end inner loop
} // end outer loop
return userNumber;
} // end getUserNumberDebugMode
Have solved the problem by clocking up a unique number check if any of the numbers match, and then putting the whole thing in a do/while loop while the unique number check > 0. Not the most elegant fix I’m sure, but it works and it’ll do.