-
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");
}
}
}
}
-
Re: Stopping while loop
use the keyword "break;" to exit any type of loops
-
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.