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?

Jcrow
  • 53
  • 2
  • 9
  • 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 :) – Siddhartha Oct 15 '15 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 – Jcrow Oct 15 '15 at 20:19
  • Does this help you? http://stackoverflow.com/questions/17538182/getting-keyboard-input – Markus Patt Oct 15 '15 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. – Jcrow Oct 15 '15 at 20:22
  • @Jcrow, as gonzo said : begin by editing your question and putting the code and explaining exactly where you are struck. – Yassin Hajaj Oct 15 '15 at 21:08
  • @YassinHajaj edited the OP with code I have tried. – Jcrow Oct 15 '15 at 21:21
  • Step through using debugger and see what values the arrays store – sam Oct 15 '15 at 21:25

1 Answers1

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");
}
pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • 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? – Jcrow Oct 15 '15 at 21:29
  • You cannot match the array objects simply. They will always be different because their addresses would be different. – pgiitu Oct 15 '15 at 21:31
  • Thanks! So what exactly stops the addresses from changing ? Is it the for loop that helps? – Jcrow Oct 15 '15 at 21:35
  • 1
    You can also use `Arrays.equals(attemptedGuess, combination)` – sam Oct 15 '15 at 21:36
  • 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. – pgiitu Oct 15 '15 at 21:38
  • @Jcrow you can also use the suggestion from @sam2090. That would basically do the same thing as the `for loop` that I have written – pgiitu Oct 15 '15 at 21:39