# Thread: Need Help Creating A Loop!

1. Member
Join Date
May 2011
Posts
1
Rep Power
0

## Need Help Creating A Loop!

Hello, I am in the process of learning Java and having some troubles. The book I am reading gave me a scenario to create a program that calculates volume. Then repeats if yes, and stops if no... This is what I have finished so far though I am receieving errors when I compile it? I cant seem to figure out what im doing wrong. All help is greatly appreciated!

here is the code,

Java Code:
```/* Protoype program to be used to prove first concepts Algorithm project */

import java.io.*;

import java.lang.*;

import java.text.*;

import java.util.*;

//Set up public class

public class Document1

{

//declare main() method

public static void main(String[] args) throws IOException

{
do
{

//Declare data types

float length,width,height,volume,loop,yes,no;

//set up input stream

Scanner readin = new Scanner(System.in);

System.out.print("\n\nCalculate the Volume by entering the height ,width, and length.\n\n\n");

//prompt for length

System.out.println("\n\n enter value for length");

//stream in length and convert to float

//prompt for width

System.out.println("\n\n enter value for width");

//stream in width and convert to float

//prompt for height

System.out.println("\n\n enter value for height");

//stream in width and convert to float

//Calculate results

volume = length*width*height;

//prepare data for out put display

DecimalFormat twodig = new DecimalFormat("########.##");

System.out.print("\n\nThe results of the calcuations are\n\n\n");

System.out.print("the Volume is:\t\t" + twodig.format(volume) + "\n\n");

yes = 0;
no = 1;

//prompt for loop

System.out.println("\n\n calculate again Yes / No ?");

}
while (loop = 0);

//stream in loop and convert to float

}//end main

}//end class```
and this was the message after compiling:

C:\Users\christian\Desktop\Document1.java:91: cannot find symbol
symbol : variable loop
location: class Document1
while (loop = 0);
^
1 error

Tool completed with exit code 1

2. You've got some pretty wonky syntax and logic problems there. For starters, you check equality with == or .equals(), not =.

But why don't you just read in a String and check that (using the equals() method) instead of using a float for some reason?

3. The compiler can't find the loop variable because it is out of scope where you use it. The scope of a variable is determined by the enclosing {}s. A variable defined inside of a pair {} is not known outside the enclosing {}.

Also (loop = 0) is an assignment statement, Not a comparsion. Use == to compare.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•