I have just embarked on a Java programming course and have been given a task to work out the volume of a cylinder in cubic inches with a given radius and height. and then to convert it from cubic inches to to cubic Cm's which i have done fine.
My problem is it wont complie with an error on one of the
System.out.print
lines that i have in the program
i have checked that i have the syntax correct and it still wont compile.
Would some one be able to give me some advice
I know this is going to sound like a totaly dumb request but hey we all have to start somewhere dont we

Dani AI

Generated

The code posted by has several clear causes for the compile error: variables are assigned before they exist (for example assigning a radius from a variable declared later), the same names are redeclared with different types, integer parsing is used where floating-point values are needed, and prompt/read order is reversed. These kinds of mistakes can make the compiler flag a later line while the real problem is earlier (as suggested). Checking for missing semicolons or unclosed braces (as hinted) is also important.

A minimal, clearer approach uses doubles and a simple input API (Scanner) and keeps prompt -> read -> parse in that order:

import java.util.Scanner;

public class CylinderVolume {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Radius (in inches): ");
        double r = in.nextDouble();
        System.out.print("Height (in inches): ");
        double h = in.nextDouble();

        double volumeIn3 = Math.PI * r * r * h;         // V = pi * r^2 * h
        double volumeCm3 = volumeIn3 * 16.387064;       // 1 in^3 = 2.54^3 = 16.387064 cm^3

        System.out.printf("Volume: %.4f in^3 = %.4f cm^3%n", volumeIn3, volumeCm3);
        in.close();
    }
}

Notes and troubleshooting: use Math.PI for best precision, parse inputs as double (or use Double.parseDouble), and avoid redeclaring variables with different types. The cubic conversion factor is exact because 1 inch = 2.54 cm by definition, so multiply in^3 by 2.54^3 (16.387064). Remove leftover test lines (for example the unrelated subtraction print) before compiling. 's idea to convert input to a double is on the right track; Scanner.nextDouble or Double.parseDouble are simpler choices for this task.

Recommended Answers

All 7 Replies

Post the error that you're getting. Often, a syntax error may not be on the line given by the message. It could be a few lines up and the effect of the error isn't seen by the parser until that line.

Check for missing semi colens ';'

That would be my first attempt at finding the problem.

Could you post the code that you've done till now ... that way we can be more helpful.

I slightly modified your post's topic to make it more polite. " Very New to Java and need help ASAP" implied that you deserve precedence over someone else's post, which is kind of considered poor etiquette. Additionally, your thread title has no relevance to your actual question. Creating a relevant thread title also helps you attract people who could help with your problem.

Just letting you know for future reference.

Hi thanks for replying to my request i have checked and double checked everything and still cant get it to compile. i hope its not too much to ask if you could have a look at this for me.
thanks i advance if you can

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class InputCircle
{

    public static void main(final String [] pArgs) throws IOException

    {

    final InputStreamReader tInputStreamReader = new InputStreamReader(System.in);
    final BufferedReader tKeyboard = new BufferedReader(tInputStreamReader);

            final double tPi = 3.141592653589793;
            double tRadius, tArea, tDiameter, tCircumfrence, tNum1,tLine1 ;
            tRadius = (tLine1);
            tArea = tPi * tRadius * tRadius;
            tDiameter = tRadius + tRadius;
            tCircumfrence = tPi * tDiameter;

    String tFirstLine = tKeyboard.readLine();
    tFirstLine = tFirstLine.trim();
    int tFirstInt = Integer.parseInt(tFirstLine);
    System.out.print("Type in a number That is the Radius of you Circle ");
    System.out.flush();

    final String tLine1 = tKeyboard.readLine();
    final int tNum1 = Integer.parseInt(tLine1);

    final String tLine2 = tKeyboard.readLine();
    final int tNum2 = Integer.parseInt(tLine2);

    System.out.println("The subtraction is" + (tNum1 - tNum2));

Thanks for letting me know the rules and stuff i am Pretty new to forums in fact very new its the first time i have ever used one. I will bear in mind what you have said if i need more help

Thanks for the Advice grifflyn

You have alot of conversion problems that need to be fixed. But even when they are fixed the program doesnt really work the way you have it. If you want to convert the input to a double do something like this:

Double d = Double.valueOf(BufferedReaderVariableName.readLine().trim());
double d2 = d.doubleValue();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.