|
-
Jul 18th, 2007, 04:20 PM
#1
Thread Starter
Member
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");
}
}
}
}
-
Jul 18th, 2007, 05:05 PM
#2
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
-
Jul 18th, 2007, 07:30 PM
#3
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|