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 12 of 12

Thread: Combination lock

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Combination lock

    For starters, I have no clue how to use the counter IV. I'm trying to create a combo lock with a 3 letter combo. in my test class I try to use the setPosition() method to try out a random or the original combo, but it cuts off after I type in the 2nd character and gives me this message "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:658)
    at CombinationLockTester.main(CombinationLockTester.j ava:27)
    Java Result: 1"

    Here's my code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import javax.swing.JOptionPane ;
    /**
     *
     * @author John
     */
    public class ComboLockTester
    {
     public static void main (String args[])
     {
     
        ComboLock MasterLock = new ComboLock("J","A","P");
       System.out.println("The combination to MasterLock is " + MasterLock.getfirst() + MasterLock.getsecond()
               + MasterLock.getthird());
     
       String input = JOptionPane.showInputDialog("Enter first character");
       MasterLock.setPosition(input);
     
     
       String input2 = JOptionPane.showInputDialog("Enter second character");
       MasterLock.setPosition(input2);
     
       String input3 = JOptionPane.showInputDialog("Enter third character");
       MasterLock.setPosition(input3);
     
       System.out.println("The user entered combo is " + input + input2 + input3);
     
       MasterLock.tryToOpen();
     
       System.out.println(MasterLock.isOpen());
     
       MasterLock.lock();
     }
    }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author John
     */
    public class ComboLock
    {
        /**
         	Creates a lock with a given combination consisting of three
    upper-case characters.
         	@param first the first letter of the combination
    	@param second the second letter of the combination
    @param third the third letter of the combination
       */
       private String first;
       private String second;
       private String third;
       private boolean open;
       private String Pos1;
       private String Pos2;
       private String Pos3;
       private int count;
     
     
     
       public ComboLock(String first, String second, String third)
       {
          open = false;
          this.first = first;
          this.second = second;
          this.third = third;
          count = 0;
       }
     
       public String getfirst()
       {
           return first;
       }
       public String getsecond()
       {
           return second;
       }
       public String getthird()
       {
           return third;
       }
     
       /**
          Set the dial to a position.
          @param aPosition a String consisting of a single uppercase
           letter (A..Z)
       */
       public void setPosition(String aPosition)
       {
           count++;
     
       if(count==0)
       {
           Pos1 = aPosition ;
       }
       else if(count == 1)
           {
               Pos2 = aPosition;
           }
       else if(count == 2)
       {
           Pos3 = aPosition;
       }
      }
     
       /**
          Try opening the lock.
       */
       public void tryToOpen()
       {
          if(Pos1.equals(first) && Pos2.equals(second) && Pos3.equals(third))
          {
              open = true;
              System.out.println("Combo correct! Lock is open.");
          }
          else
          {
              open = false;
              System.out.println("Wrong combo. Try again.");
          }
       }
     
       /**
       	 Check whether the lock is open.
       	 @return true or false indicating whether the lock is open.
       */
       public boolean isOpen()
       {
          return open;
       }
     
       /**
          Close the lock and print a message indicating that the lock is now closed
       */
       public void lock()
       {
          count = 0;
          open = false;
          System.out.println("The MasterLock is now locked");
       }
    }

    I think all i need to fix it is to find a way to let the test class know that when I call the setPosition method to link the respective characters, but like I said, I have no clue how to use counters. line 27 from that message is the following:
    [code = java]
    char Pos2 = input2.charAt(1);
    [/code]
    Thanks for any help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combination lock

    "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:658)
    at CombinationLockTester.main(CombinationLockTester.j ava:27)
    The code at line 27 used an index value past the end of the String. The code needs to check that the index value is in range. Valid indexes range from 0 to the length of the String-1. A String of length 1 does not have a second char at index 1.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Combination lock

    So the string "input2" is longer than 1 character?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combination lock

    Print out input2.length() to see how long it is.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Combination lock

    I cannot do that since I have to declare input2 before I print it, and since it is declared in the input it does not get the chance to print the length. As soon as type the second character and press enter, the program stops and only w.e is above line 27 gets executed.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combination lock

    can you post the code that shows the problem with printing it before using it? I have never seen where there is a problem that can't be solved by changing the code.
    String input2 = ...
    //  Here input2 has a value and its length can be printed

    the program stops and only w.e is above line 27 gets executed.
    What does that mean?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Combination lock

    I actually fixed the printing problem. Now the prototype program runs fully but I still can't get the arguments in the setPosition method to match the ones in the lock class. I know the counter is suppose to keep track of which user input is associated with its respective combo character so that it can tell me whether or not the original combo and the user input are the same, that is now the current problem. Thanks for the help btw. And sorry if I'm not being clear enough on the problem.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combination lock

    I still can't get the arguments in the setPosition method to match the ones in the lock class.
    Try debugging the code by adding some println statements to print out the values of the variables that are used to control the logic. The print out will show you what the computer sees when it executes the code and that should help you understand what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Combination lock

    Okay, now regardless of the input, right or wrong it gives me this message in the out put right after it prints the user input:
    "The combination to MasterLock is JAP
    The user entered combo is JAP
    Exception in thread "main" java.lang.NullPointerException
    at ComboLock.tryToOpen(ComboLock.java:80)
    at ComboLockTester.main(ComboLockTester.java:31)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 7 seconds)"

    --- Update ---

    I also updated my codes on this thread.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combination lock

    Exception in thread "main" java.lang.NullPointerException
    at ComboLock.tryToOpen(ComboLock.java:80)
    There is a variable on line 80 that had a null value when that statement was executed. Look at line 80, find the variable with the null value and then backtrack to find out why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Combination lock

    Its because the test class does not know which input goes to which position. I'm sure I have to implement the counter into the test class. I don't know how though, like in the first input, the test class does not know that tat input is associated with the first parameter of the object, in this case "J" so if i input "N" the test class cannot compare them.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Combination lock

    Time to stop and think about how the program should work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Combination Lock Class help
    By dx8292 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 4th, 2012, 10:25 AM
  2. how to obtain the combination of ip in String??
    By sxlend in forum Java Networking
    Replies: 2
    Last Post: July 15th, 2012, 01:03 PM
  3. Printing a Combination
    By ranjithfs1 in forum Loops & Control Statements
    Replies: 2
    Last Post: March 6th, 2012, 10:24 AM
  4. A deadly combination
    By grNadpa in forum Object Oriented Programming
    Replies: 5
    Last Post: February 25th, 2012, 06:20 PM
  5. Lock up code to the click of a button!!!
    By Allen Walker in forum Java Theory & Questions
    Replies: 2
    Last Post: July 1st, 2011, 09:12 AM