Results 1 to 4 of 4
  1. #1
    chrisnojen117 is offline Member
    Join Date
    May 2011
    Rep Power

    Default 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.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
    //Declare data types
    float length,width,height,volume,loop,yes,no;
    //set up input stream
    Scanner readin = new Scanner(;
    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\ cannot find symbol
    symbol : variable loop
    location: class Document1
    while (loop = 0);
    1 error

    Tool completed with exit code 1

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power


    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?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

Similar Threads

  1. Creating a loop help !
    By theman279 in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 10:13 AM
  2. creating variables in a for loop?
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 03:37 AM
  3. Help with creating a while loop!
    By BAD in forum New To Java
    Replies: 1
    Last Post: 07-09-2010, 09:00 PM
  4. Creating a Loop
    By SenorJalapeno in forum New To Java
    Replies: 3
    Last Post: 04-02-2010, 10:13 AM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM

Posting Permissions

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