Results 1 to 3 of 3

Thread: Stopping while loop

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    39

    Stopping while loop

    Hi
    Could any one tell me how to stop while loop I tried some condition it works but not exactly what I want

    Code:
    //programe to show the room number in the hotel wtih its number of guests in each room
    import java.io.*;
    import java.util.*;
    public class ShowOneRoomOccupancy
    {
    	public static void main (String[] args) 
    		throws FileNotFoundException
    	{
    		
    		int whichRoom;
    		
    		while (true)
    		{
    			Scanner scanner = new Scanner(System.in);
    			System.out.print("Which room\t?");
    			whichRoom = scanner.nextInt();
    			Scanner disckScanner = new Scanner (new File("occupancy"));
    		
    			for (int roomNum = 0; roomNum< whichRoom && disckScanner.hasNext(); roomNum++) 
    			{
    				disckScanner.nextInt();
    			}
    				if(disckScanner.hasNext())
    				{
    				System.out.print("Room"+ "");
    				System.out.print(" "+whichRoom);
    				
    				
    				System.out.print(" "+"has\t");
    				System.out.print(disckScanner.nextInt());
    				System.out.println(" "+ "guests(s).");
    	  	    
    			}
    			else
    			{
    				System.out.println("Please enter valid room number 0-9");
    			}
    		
    	}
    	
    			
        }
    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Stopping while loop

    use the keyword "break;" to exit any type of loops
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Stopping while loop

    Yeah, break is what you're looking for, but it's always best to avoid a while loop without any real condition.

Posting Permissions

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



Click Here to Expand Forum to Full Width