Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problems with a code

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with a code

    Hi,

    I'm new to this forum and new to java programming. I'm working on my 1st java program but I'm having a hard time understanding what I'm doing, and I hope that maybe someone can help me.

    I'm working on a program that calculates the volume of a box and the volume of a cylinder, following the equations V=W*H*L and V=3.14*R*R*H.

    Every time I try running the program I get multiple errors. Any help will be appreciate it. Thank you


    import java.io.*;

    import java.lang.*;

    import java.text.*;

    import java.util.*;

    //Set up public class

    public class Volumes

    {

    //declare main() method

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

    {

    //Declare data types

    float length,width,radius,3.14,height,volume of box,volume of cylinder;

    //set up input stream

    Scanner readin = new Scanner(System.in);

    //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 3.14

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

    //stream in 3.14 and convert to float

    3.14 = readin.nextFloat();

    //prompt for radius

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

    //stream in radius and convert to float

    radius = readin.nextFloat();

    //prompt for height

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

    //stream in height and convert to float

    height = readin.nextFloat();

    //Calculate results

    Volume of box = length*width*height;

    Volume of cylinder = 3.14*radius*radius*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 of box is:\t\t" + twodig.format(Volume of box) + "\n\n");

    System.out.println("Volume of cylinder is:\t\t" + twodig.format(Volume of cylinder));

    }//end main

    }//end class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Problems with a code

    I get multiple errors.
    Please post the full text of the error messages if you want help with them.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with a code

    Variables should not be declared with spaces;

    float length,width,radius,3.14,height,volume of box,volume of cylinder;

    volume of box, instead you can give volume_of_box..


    There are certain rules for identifiers. Listed below :
    1. reserved words cannot be used.
    2. they cannot start with a digit but digits can be used after the first character (e.g., name1, n2ame are valid).
    3. they can start with a letter, an underscore (i.e., "_") or a dollar sign (i.e., "$").
    4. you cannot use other symbols or spaces (e.g., "%","^","&","#").

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problems with a code

    if you look at your variable, you cannot declare as "volume of box", "volume of cylinder", and "3.14", in all volume of box or volume of cylinder has space on it, declare it without the spaces then it should be fine, and also you cannot declare like 3.14 number, that's a number not variable, you can declare some variable then initilize it as 3.14, but there's no need to declare 3.14 because it's already a number, declare a variable and initialize that variable as 3.14, then that will make sense. For example -> double Pie = 3.14;
    and also you cannot declare any variable as reserve word or with symbols which are shown in your error, if you look at your error, don't use those symbol when you declare a variable and don't use reserve word like "for, class, public, private, String, double, float etc..) are reserve word, if you really want to use these word put some number at the end or more word then it will not be same as reserve word, these reserve word means it already reserved in java to perform some other action, For example: public is a reserve word in java, if you look at your code, you started with public class Volumes, if you look in this line it's using public class words to declare your class, so java has like this many reserve words, so you cannot declare a variable with these reserve words, you will have to name to something else in order to declare, so mainly all your errors are for variable, so just switch your variable name so you won't use reserve word, and if you didn't use any reserve word, then just take out the space between the variable, because when you declare a variable it cannot have any space between the words, and rather than declare the number as 3.14, declare a variable and initilize the variable with the number, then i think it should work fine.

    Hope this helps

Similar Threads

  1. [SOLVED] n00b having problems running code
    By cha0s619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 10th, 2012, 03:31 PM
  2. phone book code problems
    By mu'min in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 16th, 2010, 10:36 AM
  3. phone book code problems
    By mu'min in forum Member Introductions
    Replies: 1
    Last Post: December 16th, 2010, 08:07 AM
  4. Problems with Code?
    By Sean137 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2010, 02:15 PM
  5. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM