This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can't Break Out of a Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic

Can't Break Out of a Loop

Dari Ko

Joined: Oct 16, 2011
Posts: 1
// Allows for input from user.
import java.util.Scanner;
// Allows for use of the printf method.

* Class that uses several different types of loops to find the factorial
* of a given number and print a table of dimensions for several rectangles.
public class LoopExamples {

public boolean replay;

// Uses a while loop to find the factorial of a given integer.
public int factorial(int i){

int factorial = 1;

while(i > 0){

factorial = factorial * i;

return factorial;

}while(replay = true);


* Asks the user if he or she would like to calculate another factorial.
* The factorial method will either quit or re-run depending on the answer.
public void again(boolean replay){

System.out.println("Would you like to calculate another factorial?");
System.out.println("Please enter Y or N:");
Scanner s = new Scanner(;
String answer = s.nextLine();
if(answer.equals("Y") || answer.equals("y"))
replay = true;
else if(answer.equals("N") || answer.equals("n"))
replay = false;
System.out.println("Please enter Y or N:");

LoopExamples a = new LoopExamples();

/* If the user wants to calculate another factorial, the user is asked for an integer,
* then the factorial method is called and the factorial given. If the user does not want
* to proceed further, then the program quits and moves on.
public void ifTrue(boolean replay){

while (replay = false) {
System.out.println("Thanks for using the Factorial Calculator!");

while(replay = true){

Scanner s = new Scanner(;
System.out.println("Please enter the integer to find the factorial of:");
int i = s.nextInt();

LoopExamples l = new LoopExamples();
int factorial = l.factorial(i);

System.out.println("The factorial of " + i + " is " + factorial + ".");

LoopExamples a = new LoopExamples();


// Method main that calls the other methods.
public static void main(String[] args) {

System.out.println("Welcome to the Factorial Calculator!");

LoopExamples a = new LoopExamples();
boolean replay = true;


This is my code.
Even if I enter "n" or "N" when asked if I want to calculate another factorial, it doesn't break. Instead it goes back and asks me to enter a new integer. Help?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 10929

while (replay = false) {

never do the above, and your non-working code is why. First, you probably meant to use '=='. but a better way would simply be:

while (!replay ) {

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
wood burning stoves
subject: Can't Break Out of a Loop
Similar Threads
urgent help needed for assignment
Need help ending number guessing game and asking if user wants to play again
two puzzling errors
Help with Value Returning Methods
using user input to pick Object to be used in case/Switch