-2

I'm writing some code for school (a test class and a primary class), and when I create a test class, the methods I call from the primary class are "undefined for type CylinderTest". When I try to run the program anyway, Eclipse can't even find the main class.

I've looked over so many stack overflow questions for the answer. I've had a similar problem here, but that doesn't fix my current problem. I've also tried (on my own) importing java.util.Scanner into the problem class, but that didn't work, either

File #1

package CirclePackage;
import java.util.Scanner;

public class Cylinder {

    public static double askForRadius() {
        System.out.println("What would you like the radius of the cylinder to be?");
        Scanner in = new Scanner(System.in);
        double r = in.nextDouble();
        in.close();
    return r;
    }

public static double askForheight() {
    System.out.println("What would you like the height of the cylinder to be?");
    Scanner in = new Scanner(System.in);
    double h = in.nextDouble();
    in.close();
    return h;
    }

public static double getVolume(double radius, double height) {
    double area = Math.PI*radius*radius*height;
    return area;
    }

}

File #2:

package CirclePackage;


public class CylinderTest {

static void main(String[] args) {

    double r = askForRadius(); //<--------------Errors appear HERE,
    double h = askForheight(); //<----------------------------HERE,
    double result = getVolume(r, h); //<------------------and HERE.
    System.out.println("The Volume of the cylinder is: " + result);

    }

}

This program is supposed to calculate the volume of a cylinder based on user inputs for the cylinder's radius and height. It does not.

Brandon_J
  • 195
  • 3
  • 17
  • main method must be `public static void main(String[] args)` and call static methods using class name (for example `Cylinder. askForRadius()`) – Ryuzaki L Dec 31 '18 at 21:45
  • 1
    You have to write `Cylinder.askForRadius()` since the method is in `Cylinder`, not in `CylinderTest`. – Progman Dec 31 '18 at 21:45
  • Now it says Error: Could not find or load main class CylinderTest.java when I try to run the program. I'll try cleaning everything again. – Brandon_J Dec 31 '18 at 21:48
  • are you doing this command line? @Brandon_J – Ryuzaki L Dec 31 '18 at 21:49
  • OK so it runs halfway through now lol, but then gives "Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at CirclePackage.Cylinder.askForheight(Cylinder.java:16) at CirclePackage.CylinderTest.main(CylinderTest.java:9)" – Brandon_J Dec 31 '18 at 21:50
  • No, with Eclipse – Brandon_J Dec 31 '18 at 21:51

2 Answers2

2

Here is the fix :

public class CylinderTest {

    public static void main(String[] args) {

        double r = Cylinder.askForRadius(); //<--------------Errors appear HERE,
        double h = Cylinder.askForheight(); //<----------------------------HERE,
        double result = Cylinder.getVolume(r, h); //<------------------and HERE.
        System.out.println("The Volume of the cylinder is: " + result);

    }

}

Second class:

import java.util.Scanner;

public class Cylinder {

    public static double askForRadius() {
        System.out.println("What would you like the radius of the cylinder to be?");
        Scanner in = new Scanner(System.in);
        double r = in.nextDouble();
        return r;
    }

    public static double askForheight() {
        System.out.println("What would you like the height of the cylinder to be?");
        Scanner in = new Scanner(System.in);
        double h = in.nextDouble();
        return h;
    }

    public static double getVolume(double radius, double height) {
        double area = Math.PI*radius*radius*height;
        return area;
    }

}
J. Adam
  • 1,457
  • 3
  • 20
  • 38
  • Trying this now – Brandon_J Dec 31 '18 at 22:38
  • Ideally, you want to specify *what* you changed and *why* you changed it. While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Cardinal System Dec 31 '18 at 22:56
2

Your main problem is that you are trying to invoke the methods askForRadius, askForheight, and getVolume, without using references in a class where they are undefined. Consequently, the compiler will search for these methods in the class CylinderTest and, upon not finding them, produce and error. You need to specify where these methods are located before the compiler can compile your code.

To do this is actually quite simple. All need to do is import the class which contains these methods and reference it when you invoke your method. To reference a class, you state its path and name, or, if it is imported, you simply state its name.

double r = Cylinder.askForRadius();
double h = Cylinder.askForheight();
double result = Cylinder.getVolume(r, h);

I suggest you do some supplementary reading on Classes and Objects, and take a look at this question: What are classes, references and objects?

The other main problem here is your main method. If you look at the Oracle Tutorial, you'll see that it says:

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

Now, if you look at your code, you'll see that your main method has the static modifier, but not the public one. You better make it public or your program will never run ;)


Alright, that solves the two main problems, but you still have two more small ones.

First of all, you are closing System.in in your askForRadius method, so you will not be able to read from it when you call your askForheight method.

Secondly, you are creating a new scanner instance every time you want to read from System.in. This is ultimately not a threat, but it is inefficient and very "dirty" code.

The solution to these next two problems is already defined in an answer on another question:

You should create just one Scanner which you use for the life of the program

and you should not close this scanner.

Cardinal System
  • 2,749
  • 3
  • 21
  • 42