# Need Help Creating A Loop!

• 05-12-2011, 05:46 PM
chrisnojen117
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,

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 length = readin.nextFloat(); //prompt for width System.out.println("\n\n enter value for width"); //stream in width and convert to float width = readin.nextFloat(); //prompt for height System.out.println("\n\n enter value for height"); //stream in width and convert to float height = readin.nextFloat(); //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"); loop = readin.nextFloat(); 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
• 05-12-2011, 05:50 PM
KevinWorkman
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?
• 05-12-2011, 05:51 PM
Norm
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.
• 05-12-2011, 07:30 PM
KevinWorkman