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

Thread: How to open the lock?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to open the lock?

    import java.util.Scanner;
     
    public class Chapter8{
     
        // user need to find the combo to open the lock
        // the right combo is first right, left and then right
     
        Scanner userInput = new Scanner(System.in);
     
        public void printMenu()
        {
     
            System.out.println("1) Choose combo");
            System.out.println("2) Reset");
            System.out.println("3) Try to open the lock ");
            System.out.println("4) Finish");
        }
     
        public void combo()
        {
            System.out.println("Type in the right code to open the lock:  ");
            String inputOne =  userInput.next();
            String inputTwo =  userInput.next();
            String inputThree = userInput.next();
            return;
        }
     
        public void reset()
        {
            userInput.reset();
            System.out.println("You have reset!");
        }
     
        public void open()
        {
            String inputOne =  userInput.next();
            String inputTwo =  userInput.next();
            String inputThree = userInput.next();
     
     
            if (inputOne.equals("right") && inputTwo.equals("left") && inputThree.equals("right")) // the code ignores this
            {
            System.out.println("You succed to open the lock");
     
            }
            else
            {
                System.out.println("You got wrong combo");
                // this line shows in the console even though the user input in the console is right
     
            }
        }
    }

    Main

    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Chapter8 Chapter8 = new Chapter8();
     
            boolean running = true;
     
            do
            {
               Chapter8.printMenu();
               int choice = Chapter8.userInput.nextInt();
                switch(choice)
                {
            case 1:
                Chapter8.combo();
                break;
            case 2:
                Chapter8.reset();
                break;
            case 3:
                Chapter8.open();
                break;
            }
        } while(true);
    }
    }
    Last edited by 19xx62xx14; October 4th, 2021 at 05:32 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: How to open the lock?

    Can you copy and paste here the contents of the console from when you execute the code that shows the problem.
    Add some comments to the posting where you the output is not what you expected.


    One possible problem with the code is it has more than one instance of the Scanner class that reads from the console. If there is any input read and buffered by one instance, another instance of Scanner will not see it. Better to only have one instance of the Scanner to read user input from the console.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to open the lock?

    Its a logical problem so it does not pop up anything in the console. I have put some comments in my code, I also think it has something to do with the user input and scanner but I tried different ways and it wont work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: How to open the lock?

    it does not pop up anything in the console
    That is the problem.
    If the program is trying to read user input I would expect it to print something on the console telling the user what to enter.
    The program needs to ask for user input before trying to read the user's input.

    All the calls to Scanner methods need to be inside of a method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to open the lock?

    1) Choose combo
    2) Reset
    3) Try to open the lock
    4) Finish
    1
    The lock has a three combo code. First right or left?
    right
    Second. Do you want to turn right or left?
    left
    Third. Do you want to turn right or left?
    right
    1) Choose combo
    2) Reset
    3) Try to open the lock
    4) Finish
    3
    You got wrong combo
    1) Choose combo
    2) Reset
    3) Try to open the lock
    4) Finish

    This comes up now that I fixed that but evenif the user types in right combo the lock wont open

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: How to open the lock?

    Is that the current contents of the console when the program is run?
    What is wrong with it? What should it be different?

    Add some comments to the output to show where the output is not what is expected. For example:
    <<<< This should be ....


    Also post the new code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to open the lock?

    To open the lock the user need to type right left right and it should pop up a message that the lock is opened and thats the problem. Even if input is right left right the message dont pop up. It ignores the if and hops over to else.

    The code is updated now also
    Last edited by 19xx62xx14; October 2nd, 2021 at 08:14 AM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: How to open the lock?

    What is the shown on the console when the new code is executed?
    Add some comments to the output showing where it is not what is expected.

    You missed this:
    All the calls to Scanner methods need to be inside of a method.
    There are calls to Scanner methods outside of any method:
     
        String inputOne =  userInput.nextLine();
        String inputTwo =  userInput.nextLine();
        String inputThree = userInput.nextLine();
    These statements need to be inside of a method.

    Also this has not been fixed:
    One possible problem with the code is it has more than one instance of the Scanner class that reads from the console. If there is any input read and buffered by one instance, another instance of Scanner will not see it. Better to only have one instance of the Scanner to read user input from the console.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to open the lock?

    So thank you it worked much better now. But now the problem is that the user need to type in 2 times to get the code work. I want the method combo() to save input so that I can use it again in open() method but I dont know how. Thank you btw for your patience Im new and trying to learn. The new code is up now

    1) Choose combo
    2) Reset
    3) Try to open the lock
    4) Finish
    1
    The lock has a three combo code:
    right left right
    1) Choose combo
    2) Reset
    3) Try to open the lock
    4) Finish
    3
    right left right
    You succed to open the lock
    1) Choose combo
    2) Reset
    3) Try to open the lock
    4) Finish

Similar Threads

  1. Replies: 0
    Last Post: October 14th, 2013, 08:30 PM
  2. Class level Lock vs Object level lock
    By hs82 in forum Java Theory & Questions
    Replies: 0
    Last Post: October 14th, 2013, 10:17 AM
  3. Combination lock
    By Pinares in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 26th, 2013, 06:56 PM
  4. Combination Lock Class help
    By dx8292 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 4th, 2012, 10:25 AM
  5. Replies: 1
    Last Post: August 3rd, 2012, 11:46 AM