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

Thread: How to exit while loop

  1. #1
    Junior Member
    Join Date
    May 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to exit while loop

    Hi guys this is an assignment which was assigned to me for my programming class. The code below asks the user what type of concert tickets they would like to buy once they select a option it say prints out you have selected ..... do you wish to proceed? once the user clicks yes "y" it goes into my loop, here is where the problem comes in, it continues to ask the user if they would like to buy more tickets, which is okay, but once they user says no "n" I want it to bring the menu back up and display the different options to the user again, so my question is how do I get out of this while loop? check out the code below. case B is where the loop is once i have figured that out I will apply the loop to the rest of the cases.

    public class JohnTickets
    { // opening of the JohnTickets class
    Scanner sc = new Scanner(System.in); // Declaring Scanner
    String userChoice = "";
    char category = 0;
    String Selector = "";
    String Proceed = "";
    int tickets = 0;
    int sum = 0;
    int VIPprice = 950;
    int TopDeck = 650;
    int BackView = 500;
    int newPurchase = 0;
    int newJLsubtotal= 0;
    int oldJLsubtotal = 0;
    GettersandSetters johny = new GettersandSetters(); // creating a new object of the GettersandSetters class
    // Declaration of variables

    public void JohnLegendMenu()
    { // opening of the JohnLegendMenu method
    System.out.println("====Ticket category available====");
    System.out.println("==== for the Legend Concert ====");
    System.out.println("A) VIP: R950.00 per person");
    System.out.println("B) Top Deck View: R650.00 per person");
    System.out.println("C) Back seat view: R500 per person");
    System.out.println(""); // empty line
    System.out.println("Please select a category using the number or enter X to go back to main menu ___");
    userChoice = sc.nextLine();
    category = userChoice.charAt(0);
    JohnLegendTicketPrice();
    } // closing of the JohnLegendMenu

    public void JohnLegendTicketPrice()
    { // opening of the JohnLegendTicketPrice method
    switch (category)
    { // opening of the catergory switch statement
    case 'a':
    {
    System.out.println("You have chosen the VIP tickets");
    EmptyLine();
    System.out.println("Press Y to proceed or N to go back to the main menu___");
    Proceed = sc.nextLine();
    if(Proceed.equalsIgnoreCase("y"))
    {
    EmptyLine();
    System.out.println("Please enter how many tickets you would like to buy");
    tickets = sc.nextInt();
    // creating a new object of the GettersandSetters class
    johny.JohnLegendTicketsSetter(tickets); // calling the ticket class which accepts input "tickets"
    sum = VIPprice * johny.JohnLegendTicketGetter();
    johny.JohnLegendSetter(sum);
    EmptyLine(); // skips a line
    System.out.println("The subtotal of the John Legend Tickets is: " + sum);
    System.out.println("Press Y to buy more Legend tickets or N to go to main menu___");
    EmptyLine();
    if(Proceed.equalsIgnoreCase("y"))
    {


    }
    if(Proceed.equalsIgnoreCase("n"))
    {
    JohnLegendMenu();
    }

    break;
    }
    if(Proceed.equalsIgnoreCase("n"))
    {
    JohnLegendMenu();
    }
    break;
    }
    case 'b':
    System.out.println("You have chosen top deck view");
    EmptyLine();
    System.out.println("Press Y to proceed or N to go back to the main menu___");
    Proceed = sc.nextLine();
    if(Proceed.equalsIgnoreCase("y"))
    {
    EmptyLine();
    System.out.println("Press Y to proceed or N to go back to the main menu___");
    while (Proceed != "y")
    {
    System.out.println("Please enter how many tickets you would like to buy");
    tickets = sc.nextInt();
    johny.JohnLegendTicketsSetter(tickets);
    newPurchase = TopDeck * johny.JohnLegendTicketGetter();
    oldJLsubtotal = johny.JohnLegendGetter();
    newJLsubtotal = oldJLsubtotal + newPurchase;
    johny.JohnLegendSetter(newJLsubtotal);
    EmptyLine();
    System.out.println("The subtotal for John Legend is: " + newJLsubtotal);
    EmptyLine();
    System.out.println("Press Y to proceed or N to go back to the main menu___");
    break;
    }
    if(Proceed.equalsIgnoreCase("n"))
    {
    JohnLegendMenu();
    }
    break;
    }
    if(Proceed.equalsIgnoreCase("n"))
    {
    JohnLegendMenu();
    }
    break;
    case 'c':
    System.out.println("You have chosen the back view");
    EmptyLine();
    System.out.println("Press Y to proceed or N to go back to the main menu");
    Proceed = sc.nextLine();
    if(Proceed.equalsIgnoreCase("y"))
    {
    EmptyLine();
    System.out.println("Please enter how many tickets you would like to buy");
    tickets = sc.nextInt();
    johny.JohnLegendTicketsSetter(tickets);
    sum = BackView * johny.JohnLegendTicketGetter();
    johny.JohnLegendSetter(sum);
    System.out.println("The subtotal of the John Legend Tickets is: " + sum);
    System.out.println("Press Y to proceed or N to go back to the main menu___");
    if(Proceed.equalsIgnoreCase("y"))
    {

    }
    if(Proceed.equalsIgnoreCase("n"))
    {
    JohnLegendMenu();
    }
    break;
    }
    if(Proceed.equalsIgnoreCase("n"))
    {
    JohnLegendMenu();
    }
    case 'x':
    JohnLegendMenu();
    break;
    default:
    System.out.println("You have chosen a invalid option");
    } // closing of the catergory switch statement
    } // closing of the JohnLegendTicketPrice method

    public static void EmptyLine()
    {
    System.out.println("");
    } // This method skips a line to create a more user friendly interface and experience

    } // closing of the JohnTickets class

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: How to exit while loop

    how do I get out of this while loop?
    A break statement will cause execution to exit a loop.


       while (Proceed != "y")
    Note: Use the equals() method to compare String values, not the == or != operators

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

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

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to exit while loop

    Hi, I think you misunderstood the question, if the user says no "n" the JohnLegendMenu method should be called, any ideas on how to achieve this? thanks for the useful advice on comparing strings.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: How to exit while loop

    if the user says no "n" the JohnLegendMenu method should be called
    Where in the code is that a requirement?
    The code would follow that requirement by using an if statement:
      if (user says no) {
        call JohnLegendMenu
      }

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

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

    to get highlighting and preserve formatting.

    I don't try to read unformatted code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. System.exit(0)
    By aknessy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2013, 04:10 PM
  2. JFrame Exit Problem.
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 06:20 AM
  3. while loop wont exit
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:42 AM
  4. Can't seem to understand why my while loop doesn't exit!
    By vortexnl in forum Loops & Control Statements
    Replies: 6
    Last Post: February 11th, 2011, 02:43 PM
  5. While Loop Exit with String
    By Zaroth in forum Loops & Control Statements
    Replies: 8
    Last Post: November 9th, 2009, 03:20 PM