Hi, I am new to Java, and have been trying to write a program that calculates the square root of a number using the Babylonian/Heron method. I have been able to make it work, largely due to a lot of help from a PhD student.
So yeah, what I'm trying to do now is to create a do-while loop so that a user chooses whether they want to exit the program or not.

So far i've got

System.out.println("Press y to continue using the program or any other character to exit");
		s = stdin.read();
		System.out.println( );
		while (s = "y"){

Where s is the variable name of whatever the user's response is. I want to say if the user presses y, for the whole program to run again, and if they don't, for the program to terminate.

All help is appreciated, thanks guys.

P.S. the relevant java class files are attached.

// This is a program to calculate the square root of any positive number

import java.io.*;

public class Root2 {

	public static void main(String[] args) throws IOException {
		//Main program 
		BufferedReader stdin = new BufferedReader( new InputStreamReader (System.in));
		// Variable declarations
		double a;
		char s;
		// Begin Program
		do {
		do {

			System.out.print("Welcome to my Square Root Program ");

			System.out.println( );

			System.out.print("Please enter the number you want to find the square root of: ");
			a = Double.parseDouble(stdin.readLine());

			if (a <=0)
				System.out.println("INVALID. Please enter a POSITIVE real number.");

				System.out.println( );


		while(a <= 0);

		double x = 0;
		RootApproximator2 r = new RootApproximator2(a);
		while (r.hasMoreGuesses())
		x = r.nextGuess();

		System.out.println( );
		System.out.println("The square root of " + a + " is " + x);
		System.out.println("Do you want to continue using the program?");
		System.out.println( );
		System.out.println("Press y to continue using the program or any other character to exit");
		s = stdin.read();
		System.out.println( );
		while (s = "n"){
		System.out.println("Hope you liked my program, good bye!");

// A class that outlines the Heron Method for calculating square roots
// It is to be used with the 'Root2' Java class

public class RootApproximator2 {

private double x;
private double guess;
private double previousGuess = 0;
private double user1;

public RootApproximator2(double a){

guess = 1;
user1 = a;

public double nextGuess(){
previousGuess = guess;
guess = (previousGuess + (user1/previousGuess))/2;

return guess;


public boolean hasMoreGuesses(){
if(Math.abs(guess - previousGuess)<(1e-8))
return false;


return true;

10 Years
Discussion Span
Last Post by Thinka

From your code:

while (s = "n"){

This is wrong in two aspects:
1. You are trying to do an assignment operation instead of comparison.
2. Even if you want to compare Strings, you should be using the equals() method instead of ==


Hey aniseed, i'm using Eclipse, and it doesn't seem to like me using "equals()".

It also has a problem with my reading in line, i've got


and it told me that it couldn't convert an integer to a character (which sounds reasonable enough!)
Any ideas as to how to make that a character reading in line?


Eclipse likes equals a lot... What you're doing, comparing object references using ==, is a deadly sin almost universally (there are a very few scenarios where you want it, but those are so specific you can for now forget about them).

In general: NEVER use == to compare objects, it doesn't work (or rather it works perfectly, just not how you think it does).


OK jwenting, thanks, I changed the type to int, so that now my code compiles and runs with no errors, but the test doesn't work :). The joys of hacking code. Anyone know or recommend any good (current) books for absolute beginners with no teachers to help out?

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.