Can't get boolean to change!!
Hi guys, I've been trying to get the boolean value in this program to change to true time and again, but I can't figure out what the problem is. I've tried everything I can, but I haven't found the problem yet. Here's what I've got so far:
Code Java:
import java.util.Scanner;
public class Combo {
private String s1;
private String s2;
private String s3;
private boolean open;
private String attemptedCombo;
public Combo() { }
public String setCombo() {
Scanner in = new Scanner(System.in);
System.out.println ("Please enter the first digit of your combo:");
s1 = in.nextLine();
System.out.println ("Please enter the second digit of your combo:");
s2 = in.nextLine();
System.out.println ("Please enter the final digit of your combo:");
s3 = in.nextLine();
System.out.println("The combination is now " + s1 + s2 + s3 + ".");
return "The combination is now " + s1 + s2 + s3 + ".";
}
public boolean openLocker(){
Scanner in = new Scanner(System.in);
System.out.println ("You encounter a locker!! Enter the combination to open it and presumably get some kind of reward!!:");
attemptedCombo = in.nextLine();
if (attemptedCombo.substring(attemptedCombo.length()).equals(s3) && attemptedCombo.substring(attemptedCombo.length()-1).equals(s2) && attemptedCombo.substring(attemptedCombo.length()-2).equals(s1))
{
open = true;
System.out.println("The locker is now open.");
}
else {open = false;
System.out.println("The locker remains closed.");}
return open;
}
}
I can't get the boolean "openLocker" to change to true.
Anyone see what I'm doing wrong?
Re: Can't get boolean to change!!
attemptedCombo.substring(attemptedCombo.length()) returns the whole string, not the last digit. for that you want attemptedCombo.substring(attemptedComob.length()-1,attemptedCombo.length())
But perhaps better to use charAt or something?
Re: Can't get boolean to change!!
Thanks a bunch, that worked like a charm!!
I'm sorry, I'm really new to Java, and I'm still learning the specifics, so I appreciate the help...