0

In the following code under the test case, when I call the scrollDown() method, from within the case, the default case is executed also. When I don't call the scrollDown method, everything runs fine. Could you please help.

import java.util.*;

public class Front
{
    public static void main(String[] args)
    {
    Scanner kb = new Scanner(System.in);
    String command;
    boolean run = true;
    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nWelcome. Type help for a list of commands.");

    while(run == true)
    {
        System.out.print("Enter a command: ");
        command = kb.nextLine();
        command = command.toLowerCase();

            switch(command)
            {
                case "help":
                    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nThis is the help menu.");
                    System.out.println("Commands are \n help \n exit \n test \n");
                    break;

                case "exit":
                    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nThank you. Goodbye.");
                    run = false;
                    break;

                case "test":
                    System.out.print("Enter an interger for the test: ");
                    scrollDown(kb.nextInt());/* when I call this method the default gets executed also. When I don't call this method it works fine*/
                    System.out.println("Why?????");
                    break;

                default:
                    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\nNot a recognized command. Type help for list of commands or exit to quit.");
                    break;
                }
    }




    }

    public static void scrollDown(int num)
    {
        for (int count = 1; count <= num; count++) /* counts to the number you enter on the test case option */
        System.out.println(count);
    }

}
C W
  • 11
  • 1
  • add kb.nextLine(); after scrollDown(kb.nextInt()); – vs97 Dec 21 '18 at 01:14
  • Thank you. I haven't programmed in java since college. I knew we covered it but I couldn't remember or find it on google or find it in my old text book. So what exactly is happening? Is there a line break stored somewhere that pops up when I try to retrieve user input? – C W Dec 21 '18 at 01:33

0 Answers0