Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can't get boolean to change!!

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Location
    Boston, MA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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?


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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?
    Last edited by Zula; November 2nd, 2010 at 06:28 PM.

  3. The Following 2 Users Say Thank You to Zula For This Useful Post:

    javapenguin (November 2nd, 2010), JavaPF (November 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Location
    Boston, MA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

Similar Threads

  1. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM
  2. Need help with what I believe is Boolean & add branching
    By JavaBeginner123 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 1st, 2010, 01:55 PM
  3. Boolean Value Not Changing
    By bosox960 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 21st, 2010, 02:11 PM
  4. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM
  5. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM