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

Thread: do-while loop containing switch statement cotinues looping when end statement is supposed to be reached?

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question do-while loop containing switch statement cotinues looping when end statement is supposed to be reached?

    Hello, I feel like I've tried everything and I genuinely don't know how to fix this. The point is for the program to accept both specific commands and 1-5 as acceptable inputs as the commands are pretty long to be typing in every time. Everything seems to work up until the point the do-while loop in the "runCommands" is supposed to end when "5" or "exit" is entered, but the loop just keeps on going. Does anyone have any idea how to solve this?


    Any help would be endlessly appreciated


    import java.util.Scanner;

    public class CodeAssignment {

    private Scanner input = new Scanner(System.in);

    private void newPrintln(String prompt) {
    System.out.println(prompt);
    }

    private void programMenu() {
    newPrintln("What would you like to do?"
    + "\n1. register new person. "
    + "\n2. increase age"
    + "\n3. list people"
    + "\n4. remove person"
    + "\n5. exit");
    }


    public void run() {
    initialize();
    runCommands();
    exitProgram();
    }


    private void initialize() {
    System.out.println("Welcome to the database!");
    programMenu();


    }


    private void runCommands() {
    String choice;
    do {
    System.out.println("> ");
    choice = interpretChoice();
    excecuteChoice(choice);

    } while (choice != "exit" && choice != "5");

    }


    private String interpretChoice() {
    String choice = input.nextLine();
    return choice;
    }


    private void excecuteChoice (String choice) {
    switch (choice) {
    case "1":
    case "register new person":
    break;

    case "2":
    case "increase age":
    break;

    case "3":
    case "list people":
    break;

    case "4":
    case "remove person":
    break;

    case "5":
    case "exit":
    break;
    default:
    newPrintln("Error: no such command."
    + "\nTry again!");
    programMenu();
    }
    }




    private void exitProgram() {
    newPrintln("Goodbye!");
    input.close();

    }
    Last edited by alyss; December 10th, 2019 at 06:15 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,107
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default

    Can you copy the contents of the command prompt window and paste it here so we can see what you are talking about?

    Where is the class's main method? It is needed for testing.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Note: Use the equals method for comparing the contents of String objects, not the == or != operators.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    alyss (December 10th, 2019)

  4. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: do-while loop containing switch statement cotinues looping when end statement is supposed to be reached?

    Thank you so much for replying, here's the code in hopefully correct format. (+ main method)

     
    import java.util.Scanner;
     
    public class CodeAssignment {
     
    private Scanner input = new Scanner(System.in);
     
    private void newPrintln(String prompt) {
    System.out.println(prompt);
    }
     
    private void programMenu() {
    newPrintln("What would you like to do?"
    + "\n1. register new person. "
    + "\n2. increase age"
    + "\n3. list people"
    + "\n4. remove person"
    + "\n5. exit");
    }
     
     
    public void run() {
    initialize();
    runCommands();
    exitProgram();
    }
     
     
    private void initialize() {
    System.out.println("Welcome to the database!");
    programMenu();
     
     
    }
     
     
    private void runCommands() {
    String choice;
    do {
    System.out.println("> ");
    choice = interpretChoice();
    excecuteChoice(choice);
     
    } while (choice != "exit" && choice != "5");
     
    }
     
     
    private String interpretChoice() {
    String choice = input.nextLine();
    return choice;
    }
     
     
    private void excecuteChoice (String choice) {
    switch (choice) {
    case "1":
    case "register new person":
    break;
     
    case "2":
    case "increase age":
    break;
     
    case "3":
    case "list people":
    break;
     
    case "4":
    case "remove person":
    break;
     
    case "5":
    case "exit":
    break;
    default:
    newPrintln("Error: no such command."
    + "\nTry again!");
    programMenu();
    }
    }
     
     
     
     
    private void exitProgram() {
    newPrintln("Goodbye!");
    input.close();
     
    }
     
     
    public static void main(String[] args) {
     
    		CodeAssignment program = new CodeAssignment();
    		program.run();
     
     
    	}
     
    }


    Here are the contents of the command window:

    Welcome!
    What would you like to do?
    1. register new person.
    2. increase age
    3. list people
    4. remove person
    5. exit
    >
    1
    >
    2
    >
    3
    >
    4
    >
    5
    >
    exit
    >

    Even after 5 or "exit" is entered the loop just keeps on going.
    (Haven't done methods for the other alternatives yet, which is why everything is blank)

    --- Update ---

    update, solved it! Thank you for mentioning the string.equals, I'd forgotten about that

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. Looping through a while loop with a switch statement
    By madgame618 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 24th, 2014, 03:47 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. for-each loop and a switch statement
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2013, 02:02 PM
  5. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM

Tags for this Thread