Home > Mobile >  Method call resumes flow --Java
Method call resumes flow --Java

Time:10-24

My program calls for a menu requires the user's input to go to the correct method, and returns to the menu to allow other methods to be used or to quit the program

            System.out.println("Start of menu options");
            System.out.println("Enter your command (quit, print reverse, replace all, replace single, remove)");
            userChoice = sc.nextLine();
            if(userChoice.equalsIgnoreCase("print reverse")){
                printReverse(userString);

            }
            if(userChoice.equalsIgnoreCase("replace all")){
                replaceAll(userString);
            }
            if(userChoice.equalsIgnoreCase("remove")){
                remove(userString);
            }
            if(userChoice.equalsIgnoreCase("replace single")){
                replaceSingle(userString);
            }
            if(userChoice.equalsIgnoreCase("quit")){
                quit();
            }
            menu();

        }

The reverse() method returns the user to the menu without issue

            List<Character> temp = new ArrayList<>();
            for(char ch : a.toCharArray()){
                temp.add(ch);
            }
            Collections.reverse(temp);
            System.out.println("The new sentence is: " temp.stream().map(i->i.toString()).collect(Collectors.joining()));
            menu();
        }

But the remaining methods all seem to bypass the scanner .nextLine() because they will end the program without the looping menu() command, but on the second loop through it will prompt for user input for the scanner call.

public static void replaceAll(String a){
            System.out.println("Enter Char");
            String charToBeReplaced = sc.next();
            System.out.println("New Char");
            String charReplacement = sc.next();
            userString = a.replaceAll(charToBeReplaced, charReplacement);
            System.out.println("Your new sentence is: "   userString);
            menu();
        }

Going through the debugger I can see that userChoice is empty by the time the program gets to checking if userChoice is equal to "quit" but the program runs the two println commands

CodePudding user response:

        public static void replaceAll(String a){
            System.out.println("Enter Char");
            String charToBeReplaced = sc.nextLine();
            System.out.println("New Char");
            String charReplacement = sc.nextLine();
            userString = a.replaceAll(charToBeReplaced, charReplacement);
            System.out.println("Your new sentence is: "   userString);
            menu();
        }

conflicting next() and nextLine()

CodePudding user response:

Scanner.next has untill whitespace whereas Scanner.nextLine has untill \n.

For example) First If you input str in replaceAll method, charToBeReplaced has str
next If you input str2,charReplacement have str2 and you may input enter \n when input str2.
Because sc have \n, userChoice have \n.

reference https://www.geeksforgeeks.org/why-is-scanner-skipping-nextline-after-use-of-other-next-functions/

  • Related