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!