3

Description of Example

I'm new to Java and I'm trying to figure out how to do this example. I originally planned to store the 3 user guesses into an Array called UserGuess and store the 3 attempted guesses in another array, but I'm confused as to how I would store USER input into an array(I've only been using fixed numbers). I'm also confused as to how I would write out if the UserGuess array's integers are equal to the ones of the attempted guesses, a messagebox would appear indicating the lock is opened or the attempted guesses were wrong.

package ComboLockerDrive;

import javax.swing.JOptionPane;

public class ComboLockerDrive {

public static void main(String[] args) {




    int digit1 = Integer.parseInt(JOptionPane.showInputDialog("Enter the first digit of your locker combo: "));
    int digit2 = Integer.parseInt(JOptionPane.showInputDialog("Enter the second digit of your locker combo: "));
    int digit3 = Integer.parseInt(JOptionPane.showInputDialog("Enter the third digit of your locker combo: "));
    System.out.println("The code has been set!");

     int[] combination = new int[3];
     combination[0] = digit1;
     combination[1] = digit2;
     combination[2] = digit3;

     System.out.println("Now we will try and open the lock!");

     int[] attemptedGuess = new int[3];

     int Guess1 = Integer.parseInt(JOptionPane.showInputDialog("Enter your first guess: "));
     int Guess2 = Integer.parseInt(JOptionPane.showInputDialog("Enter your second guess: "));
     int Guess3 = Integer.parseInt(JOptionPane.showInputDialog("Enter your third guess: "));

     attemptedGuess[0] = Guess1;
     attemptedGuess[1] = Guess2;
     attemptedGuess[2] = Guess3;

     if(combination == attemptedGuess) {
         System.out.println("The lock opened, congratulations, you got the combination right!");
     } 

     else {
         System.out.println("The lock does not open, the combination you tried was incorrect");
     }


}

}

This is what I have so far, no errors whatsoever, but when I enter the correct combination, it says that the combination is incorrect. Does this mean I didn't fill the array in properly?

7
  • If you create an array of size 3 called arr, then you can insert into it by doing arr[0]= user-given-numer, arr[1] = user-given-number and so on. Give it a shot and ask us if you get stuck :) Commented Oct 15, 2015 at 20:18
  • @gonzo haven't gotten very far because I'm stuck on the first couple things I should be typing :P, I will try and get some code for everyone Commented Oct 15, 2015 at 20:19
  • Does this help you? stackoverflow.com/questions/17538182/getting-keyboard-input Commented Oct 15, 2015 at 20:19
  • @MPirious I know how to get user input, but all of those examples store the input into a variable, in the example above it wants me to store each integer separately, so naturally I thought all 3 numbers should be stored into an array and not just save each integer into 3 different variables. Commented Oct 15, 2015 at 20:22
  • @Jcrow, as gonzo said : begin by editing your question and putting the code and explaining exactly where you are struck. Commented Oct 15, 2015 at 21:08

1 Answer 1

0

You cannot match two arrays simply for equality as when u do combination == attemptedGuess you are trying to match the references which would always be different. instead you should match the individual elements at the indexes.

You can basically match all the numbers and if one of them doesn't match you can say the combination is incorrect otherwise it is correct.

Something like this: replace the code

if(combination == attemptedGuess) {
     System.out.println("The lock opened, congratulations, you got the combination right!");
 } 
 else {
     System.out.println("The lock does not open, the combination you tried was incorrect");
 }

with this

        for(int i=0;i<3;i++){
            if(attemptedGuess[i]!=combination[i]){
                System.out.println("The lock does not open, the combination you tried was incorrect");
                return;
            }
        }
        System.out.println("The lock opened, congratulations, you got the combination right!");

or simply as suggested by @sam

if(Arrays.equals(attemptedGuess, combination)){
    System.out.println("The lock opened, congratulations, you got the combination right!");
}else{
    System.out.println("The lock does not open, the combination you tried was incorrect");
}
Sign up to request clarification or add additional context in comments.

6 Comments

Do you know whats wrong with my code? Mine is basically the same, except I don't have the for loop, unless it should be there?
You cannot match the array objects simply. They will always be different because their addresses would be different.
Thanks! So what exactly stops the addresses from changing ? Is it the for loop that helps?
You can also use Arrays.equals(attemptedGuess, combination)
What do you mean by what exactly stops the addresses from changing?. When you allocate an array, the array is stored at some address of the meomory. So the addresses of the two arrays would be different and your statement would always be false.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.