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

Thread: Switch Case Meny with Do while loop that doesnīt end

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch Case Meny with Do while loop that doesnīt end

    Hi

    I have a problem with a switch case meny. As selection number 5 the program will quit. I have built the meny in a do while loop that will run as long as menyChoise isnīt equal to "5", but it does not end. Please help me and explain whats wrong.

    import java.util.Scanner;
    public class UI {
     
    	public static void main(String[] args) {
     
    		String menyChoise = null;
     
    		do {
    		for (int x=1; x<50; x++) {
    			System.out.println(" ");
    		}
     
    		System.out.println("Welcome to Landegrens Hotel service.");
    		System.out.println("1. List Availible Rooms");
    		System.out.println("2. List Booked Rooms");
    		System.out.println("3. Check In Person");
    		System.out.println("4. Check Out Person");
    		System.out.println("5. Quit");
    		System.out.print("Please choose what you want to do by entering number:");
    		Scanner scan = new Scanner(System.in);
    		menyChoise = scan.nextLine();
     
    		switch (menyChoise) {
    			case "1":
    				System.out.println("You want to list all availible rooms");
    				try        
    				{
    				    Thread.sleep(2000);
    				} 
    				catch(InterruptedException ex) 
    				{
    				    Thread.currentThread().interrupt();
    				}
    				break;
    			case "2":
    				System.out.println("You want to list booked rooms");
    				try        
    				{
    				    Thread.sleep(2000);
    				} 
    				catch(InterruptedException ex) 
    				{
    				    Thread.currentThread().interrupt();
    				}
    				break;
    			case "3":
    				System.out.println("You want to Check In a person");
    				try        
    				{
    				    Thread.sleep(2000);
    				} 
    				catch(InterruptedException ex) 
    				{
    				    Thread.currentThread().interrupt();
    				}
    				break;
    				case "4" :
    				System.out.println("You want to check out a person");
    				try        
    				{
    				    Thread.sleep(2000);
    				} 
    				catch(InterruptedException ex) 
    				{
    				    Thread.currentThread().interrupt();
    				}
    				break;
    			case "5" :
    				System.out.println("Quiting Program. Se Yaah");
    				try        
    				{
    				    Thread.sleep(2000);
    				} 
    				catch(InterruptedException ex) 
    				{
    				    Thread.currentThread().interrupt();
    				}
    				break;
    			default:
    				System.out.println("Please choose a menynumber from 1-5");
    				try        
    				{
    				    Thread.sleep(2000);
    				} 
    				catch(InterruptedException ex) 
    				{
    				    Thread.currentThread().interrupt();
    				}
    				break;
    		}
     
    		}while (menyChoise != "5");
     
    	}
     
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Switch Case Meny with Do while loop that doesnīt end

    You are are adding " ", so it has became String instead of int/number.

    To compare String, you have to use .equals();

    while (!menyChoise.equals("5"));   // not equals
    Whatever you are, be a good one

Similar Threads

  1. switch case
    By sathirish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 10th, 2014, 04:08 AM
  2. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:03 AM
  3. Replies: 9
    Last Post: February 24th, 2013, 06:51 PM
  4. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  5. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM