theADOLESCENT Posted April 10, 2013 Posted April 10, 2013 Hey guys. I'm almost finished with my program but I can't figure out what's not working. It works when I fill in the entries in the code but when I try to use the Scanner class to have a user input the characters, and then enter a space to stop, it doesn't work. I'm guessing there's something wrong with my loop since when I debug, I can enter a letter once, it sets the first entry to that letter, but cannot keep going after that in debugger. import java.util.Scanner; public class CombinationLockTester { /** * @param args */ public static void main(String[] args) { CombinationLock lock = new CombinationLock("ABC"); Scanner scan = new Scanner(System.in); String entryString = null; char entryChar = 0; { entryString = scan.nextLine(); entryChar = entryString.charAt(0); lock.setEntry(entryChar); } while(entryChar != ' '); /* lock.setEntry('A'); lock.setEntry('B'); lock.setEntry('C'); lock.setEntry('F'); lock.setEntry('A'); lock.setEntry('B'); lock.setEntry('C'); lock.setEntry('B'); lock.setEntry('C'); lock.setEntry('D'); */ //if space entered, try to open lock boolean didOpen = lock.openLock(); if(didOpen) System.out.println("The lock opened."); else System.out.println("The lock did not open."); lock.closeLock(); } } /** A lock like object that opens when the correct 3 letter combination is entered. * @author roseka27 * */ public class CombinationLock { private final String correctCombination; private char entry1; // most recently entered letter private char entry2; private char entry3; // oldest entry /** A constructor that creates the combination lock. * @param correctCombination The 3-letter combination to open lock. */ public CombinationLock(String correctCombination) { this.correctCombination = correctCombination; } /**Set a letter as the next piece of the combination being entered. * The last three calls to this method determine whether or not the lock is unlocked. * If this is called more than three times, only the last three calls matter. * @param entry Current letter being entered for entry. */ public void setEntry(char entry) { entry3 = entry2; entry2 = entry1; entry1 = entry; } /**Tries to open the lock. Will fail if the last three entries are not equal to the lock's built in combination. * @return True if the lock successfully opens, false if it doesn't open, such as with an incorrect combination. */ public boolean openLock() { if(correctCombination.charAt(0) == entry3 && correctCombination.charAt(1) == entry2 && correctCombination.charAt(2) == entry1) return true; else return false; } /**Resets the lock to an unset state where no entries have been entered. * */ public void closeLock() { entry1 = 0; entry2 = 0; entry3 = 0; } }
pwagen Posted April 10, 2013 Posted April 10, 2013 I'm not 100% sure, since I'm not a Java pro, but I wonder if this actually works the way you intend: { // ...code... } while (entryChar != ' '); You might want to make that a do-while loop instead, otherwise I'm guessing that it only runs it once, then hangs on the while. do { // ...code... } while (entryChar != ' ');
theADOLESCENT Posted April 10, 2013 Author Posted April 10, 2013 hahah I forgot the do in there for my do while loop. Thanks, it exits on a space.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now