Keep the program running (looping)

which I am having trouble with

Page 1 of 1

6 Replies - 3040 Views - Last Post: 12 April 2007 - 12:40 AM Rate Topic: -----

#1 stew_downunder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 07

Keep the program running (looping)

Post icon  Posted 11 April 2007 - 04:26 AM

I have constructed one of my first (novice) calculator programs, and I am not real schooled up on getting this to repeat over (rather than have the prgram end and then have to restart it every time to perform another calculation.

Please help as I don't know how to do this yet!

At the moment, my code will compile but it goes straight to the "Press any key to continue" prompt.

Check out my code and pay attention to the for loop.

import java.io.*;

public class CalculateBox
{
	public static void main(String args []) throws IOException
	{
		String strHeight = "0", strWidth = "0";
		float height = 0, width = 0, total = 0;
		boolean heightDone = false, widthDone = false;
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

		for(int j = 0; j > 0; System.exit(0))
		{

		String yesOrNo;

		while(!heightDone)
		{
			try
			{
				System.out.println();
				System.out.println("Enter the height of the box: ");
				System.out.println();
				strHeight = dataIn.readLine();
				height = Float.parseFloat(strHeight);
				if(height <= 0) throw new NumberFormatException ();
				else heightDone = true;
			}

			catch(NumberFormatException e)
			{
				System.out.println();
				System.out.println("Invalid height entry! - Try again");
			}
		} // end of while

		while(!widthDone)
		{
			try
			{
				System.out.println();
				System.out.println("Enter the width of the box: ");
				System.out.println();
				strWidth = dataIn.readLine();
				width = Float.parseFloat(strWidth);
				if(width <= 0) throw new NumberFormatException ();
				else widthDone = true;
			}

			catch(NumberFormatException e)
			{
				System.out.println();
				System.out.println("Invalid width entry! - Try again:");
			}
		} // end of while

		total = height * width;

		System.out.println();
		System.out.println("The area = " + total);
		System.out.println();

		// this ain't a workin'
		//=====================
		System.out.print("Another calculation? y/n");
		yesOrNo = dataIn.readLine();
		if(yesOrNo == "n") j++;
		//=====================

		} // end of for loop

	} // end of main

} // end of class CalculateBox


Is This A Good Question/Topic? 0
  • +

Replies To: Keep the program running (looping)

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Keep the program running (looping)

Posted 11 April 2007 - 04:43 AM

I'm thinking an easier way to do this would be to replace that for loop with another while loop. create a boolean called "done" or "quit" or something like that and set it to false. Then: "while(!done) { ...", and set done to true when the user doesn't want to continue.
Was This Post Helpful? 0
  • +
  • -

#3 stew_downunder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 07

Re: Keep the program running (looping)

Posted 11 April 2007 - 05:05 AM

ok done that, now the program will run but now whether i enter a yes or a no it now keeps returning to the "The area is ..." Another calculation y/n" over and over...

import java.io.*;

public class CalculateBox
{
	public static void main(String args []) throws IOException
	{
		String strHeight = "0", strWidth = "0";
		float height = 0, width = 0, total = 0;
		boolean heightDone = false, widthDone = false;
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

	   	String yesOrNo;
	   	boolean quit = false;
	   	while(!quit)
	   	{


			while(!heightDone)
			{
				try
				{
					System.out.println();
					System.out.println("Enter the height of the box: ");
					System.out.println();
					strHeight = dataIn.readLine();
					height = Float.parseFloat(strHeight);
					if(height <= 0) throw new NumberFormatException ();
					else heightDone = true;
				}

				catch(NumberFormatException e)
				{
					System.out.println();
					System.out.println("Invalid height entry! - Try again");
				}
			} // end of height while

			while(!widthDone)
			{
				try
				{
					System.out.println();
					System.out.println("Enter the width of the box: ");
					System.out.println();
					strWidth = dataIn.readLine();
					width = Float.parseFloat(strWidth);
					if(width <= 0) throw new NumberFormatException ();
					else widthDone = true;
				}

				catch(NumberFormatException e)
				{
					System.out.println();
					System.out.println("Invalid width entry! - Try again:");
				}
			} // end of width while

			total = height * width;

			System.out.println();
			System.out.println("The area = " + total);
			System.out.println();

			// keeps looping back to here for some reason!?
			//=====================
			System.out.print("Another calculation? y/n");
			yesOrNo = dataIn.readLine();
			if(yesOrNo == "n") quit = true;
			//=====================

		} // end of quit while loop

	} // end of main

} // end of class CalculateBox

Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Keep the program running (looping)

Posted 11 April 2007 - 05:49 AM

Yup, because your heightDone and widthDone variables are still set to true, so it just skips the part where it should input the parameters.
Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Keep the program running (looping)

Posted 11 April 2007 - 06:32 AM

Instead of using yesOrNo == "n", try yesOrNo.equalsIgnoreCase("n");

Edit: Here is a page explaining the difference between the == operator and the equals method: http://www-128.ibm.c...j-ebb0917a.html
The equalsIgnoreCase String method is basically the same thing as the equals method with one obvious difference.

This post has been edited by alcdotcom: 11 April 2007 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Keep the program running (looping)

Posted 11 April 2007 - 08:05 AM

lol, and that too.
Was This Post Helpful? 0
  • +
  • -

#7 stew_downunder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 04-April 07

Re: Keep the program running (looping)

Post icon  Posted 12 April 2007 - 12:40 AM

Thanks alcdotcom and 1lacca works like a gem now! :)

This post has been edited by stew_downunder: 12 April 2007 - 12:42 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1