I am trying to write a program that will give me the volume of a cylinder when I run it. I already have a Circle class that computes the area of a circle. I am also using a seperate program to handle the input and output. Could someone tell me what I am doing wrong with my program?

public class Cylinder
{
// calls up and creates an object from the Circle class

Private Circle base;
Private double Height;

//Constructor
Public Cylinder(double h, double r)
{
// creates object from cirle class
Circle base = new circle(r);
height = h;
}
public double getVolume()
{
return CircleBase.area * height;
}
}

Dani AI

Generated

Good progress so far — the style/case fixes and mentioned remove a lot of common compile-time noise. The remaining "Reached end of file while parsing" is the compiler telling you it hit the end of the source while still expecting tokens. That almost always comes from a structural problem (an opening brace never seen, an unterminated block comment, or stray characters introduced when copying/pasting).

From the code you posted the most likely culprit is the class body not being opened where the compiler expects it. In Java, member declarations must live inside the class braces. If the opening brace for the class is misplaced or missing the parser will see field declarations as "outside" the class and then run out of file trying to finish parsing. Also watch for accidental characters (for example a stray greater-than sign or HTML entity) and for creating a local variable that hides the field you declared — assign to the field instead of shadowing it, and use the circle object's public method to obtain area rather than trying to use a non-existent field.

Practical steps to resolve:

  • Open the file in a plain-text editor or an IDE with brace matching. Look for unbalanced braces and unclosed /* comments. Use the editor's "match bracket" or code folding to find where the structure breaks.
  • Re-type the class declaration line and the following brace on a fresh file to remove invisible characters or stray HTML.
  • Compile the single source file with javac and follow the first error; fix it and recompile until no parser errors remain.
  • If stuck, reduce the file to the minimal class skeleton and add members back one at a time to isolate the problem.

Using an IDE as suggested will make these issues obvious quickly. Once the braces and scoping are fixed the other small naming/visibility issues should be straightforward to tidy.

Recommended Answers

All 12 Replies

What are you having a problem with? You told us what your program is supposed to do, and that its not working, but you never told us exactly what part isn't working.

I am sorry, I am new XP

i keep getting "Reached end of file while parsing" and all the brackets are there. What am I doing wrong!

looking at your code... did you copy and paste that? if yes then, you need to ensure that "public" and "private" modifiers are in lowercase.

Private and Public should be changed to private and public. They are supposed to be lowercase - I think it will give you a compiler error otherwise.

Private and Public should be changed to private and public. They are supposed to be lowercase - I think it will give you a compiler error otherwise.

that's what i said...and yes it will definitely give you a compiler error

Oh, sorry man. Didn't mean to step on your toes, I don't know why I didn't read that part of your response or didn't register it or something.

Edit: Also, I don't know why somebody would down vote my post. I am a regular poster here and I never 'repeat responses' or anything like that - I don't even respond if I have nothing to contribute - this was obviously an accident.

Thanks you guys. I fixed the cases, but I still get the "reached end while parsing error.

One thing I should note is that when I do get that error I also get

Public class cylinder>
^

directly underneath it

I don't see any other errors. . can you re-post your updated code? It seems like it still thinks your case is wrong somewhere.

when you created your Circle class you had your "circle" in lowercase it should be in Uppercase.

Circle base = new Circle(r);

here is my new code(with all of your suggestions) and the code for my circle class

public class Cylinder


// calls up and creates an object from the Circle class	

	private Circle base;	
	private double Height;
{
//Constructor	
	public Cylinder(double h, double r)
	{
// creates object from cirle class	
	Circle Base = new Circle(r);
	height = h;
	}
	
public double getVolume()
	{
	return CircleBase.area * height;
	}
}
public class Circle
{
  private double radius;

  public Circle(double r)
  {
    radius = r;
  }

  public double getArea()
  {
    return Math.PI * radius * radius;
  }
}

private double Height;

It looks like height should be lowercase there, according to the rest of your class. Btw, making variables lowercase to start with is a convention in Java.

I suggest you use a good JAVA editor and/or an IDE (Integrated Development Environment)...Eclipse, NetBeans or BlueJ to name a few. These errors are easily recognized by these editors and they can give you possible solutions to your errors too. It can even remind you of some JAVA conventions as what BestJewSinceJC has pointed out. If you are really serious in your programming, I strongly suggest using an IDE.

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.