Results 1 to 5 of 5
  1. #1
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Question Keeps on Looping when it is not supposed to

    Dear Fellow Javers,
    I am trying to made a code that respond to emergencies, but for some reason, it keeps on looping the menu.
    Please guide me so I can fix this problem and lead me to success.

    Thank you
    - SimeonTG

    Java Code:
    // Creating our help class
    class helpDemo {
    	void helpOn (int what) {
    		switch (what) {
    		case '1': 
    			System.out.println("Calling Emergency right now");
    			break;
    		case '2':
    			System.out.println("So it seems you don't have an emergency...");
    			break; 
    		case '3':
    			System.out.println("Just scheduled an appointment with your local urgent care...");
    		}
    		System.out.println();
    	}
    	
    	// Creating our class that shows the menu
    	void showMenu(){
    		System.out.println("What do you need help on today?");
    		System.out.println("Press 1 for Emergency help");
    		System.out.println("Press 2 for if you don't have an emergency");
    		System.out.println("Press 3 for if your emergency is minor");
    	}
    	
    	// Check if what the user types is a valid number or not 
    	boolean isValid(int ch) {
    		if(ch < '1' | ch > '7') return false;
    		else return true; 
    	}
    
    }
    
    // Now to put our class into action 
    public class HelpOnDemo {
    
    	public static void main(String[] args) 
    			 throws java.io.IOException {
    		 char choice, ignore; 
    		 helpDemo Log = new helpDemo(); 
    		 
    		 for(;;) {
    			 
    			 
    			 do {
    				 Log.showMenu();
    				 
    				 choice = (char) System.in.read();
    				 
    				 do {
    					 	ignore = (char) System.in.read();
    				    } while (ignore != '\n');
    				 
    			 }while ( !Log.isValid(choice) );
    		 
    			
    			 
    		 }
    
    	}
    
    }
    The Output:
    XML Code:
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor
    1
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor
    2
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor
    3
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor
    1
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor
    2
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor
    3
    What do you need help on today?
    Press 1 for Emergency help
    Press 2 for if you don't have an emergency
    Press 3 for if your emergency is minor

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Keeps on Looping when it is not supposed to

    it keeps on looping the menu.
    I see 3 loops in the code. Which one are you talking about?
    Check the condition that allows the loop to exit. Does its value ever change to allow the loop to exit?

    Try debugging the code by Adding some println statements that show the values of the variables used to control the loop so you can see what the computer sees when the code executes. The print out should show you where the code is going wrong.
    Last edited by Norm; 11-11-2015 at 06:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Keeps on Looping when it is not supposed to

    Thanks. Will try it.

  4. #4
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Keeps on Looping when it is not supposed to

    But how do I make it so that when you type 1, 2, or 3 that it can show a specific response and then break?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Keeps on Looping when it is not supposed to

    Write the code to do that just like you described it:
    Java Code:
    when you type 1, 2, or 3
        that it can show a specific response
        and then break?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Am I supposed to use the indexOf method?
    By son012189 in forum New To Java
    Replies: 5
    Last Post: 12-07-2011, 10:01 PM
  2. I don't understand what I'm supposed to do...
    By colerelm in forum New To Java
    Replies: 2
    Last Post: 10-04-2011, 03:01 AM
  3. small method isn't supposed to go under 0
    By senca in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 01:23 PM
  4. Values change when they're not supposed to
    By m00nchile in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 04:16 PM
  5. Help interpreting what a class is supposed to do
    By bornwithnoname in forum New To Java
    Replies: 2
    Last Post: 11-20-2008, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •