-
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
-
Re: Problems with a code
Quote:
I get multiple errors.
Please post the full text of the error messages if you want help with them.
-
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 :
- reserved words cannot be used.
- they cannot start with a digit but digits can be used after the first character (e.g., name1, n2ame are valid).
- they can start with a letter, an underscore (i.e., "_") or a dollar sign (i.e., "$").
- you cannot use other symbols or spaces (e.g., "%","^","&","#").
-
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