-1

Pretty sure that I'm missing something fairly obvious here, but I've only been at it a few days. Any help would be much appreciated. I managed to get my class with methods I would like to use to compile, by my main method I was planning link all my results to is getting an error. It says my actual arguments and formal are different. I assume I didn't connect them to the other class or something, but I'm kind of lost. Thanks again!

public class SphereAndCone {
    public static void main(String[] args) {
        Calc calc1 = new Calc();
        Scanner input = new Scanner(System.in);
        System.out.print("What is the radius?");
        // set sphere/cone radius
        double r = input.nextDouble();
        System.out.print("What is the height?");
        // set sphere/cone height
        double h = input.nextDouble();
        // print results while calling on methods
        System.out.printf("Sphere Volume: %3f16" + calc1.sphereVolume());
        System.out.println();
        System.out.printf("Sphere Surface: %3f16" + calc1.sphereSurface());
        System.out.println();
        System.out.printf("Cone Volume: %3f16" + calc1.coneVolume());
        System.out.println();
        System.out.printf("Cone Surface: %3f16" + calc1.coneSurface());
        System.out.println();
    }
}
Compass
  • 5,867
  • 4
  • 30
  • 42
  • Copy and paste the exact exception. – Compass Oct 31 '14 at 17:02
  • Im getting: System.out.print f("Sphere Surface: %3f16" + calc1.sphereSurface(r)); ^ required: double,double found: double reason: actual and formal argument lists differ in length 2 errors – getNoob.java Oct 31 '14 at 17:04
  • Try using [this answer](http://stackoverflow.com/a/18732497/2958086). You should be providing 2 (or more, if you have multiple formatted values) arguments to printf, not one. Likely you want to replace the `+` with `,` – Compass Oct 31 '14 at 17:08
  • Thanks so much! I'll check that out and give it a try :) – getNoob.java Oct 31 '14 at 17:09
  • Yes, you need to use a comma (`','`) instead of a plus (`'+'`). – Mr. Polywhirl Oct 31 '14 at 17:09
  • `System.out.printf("Sphere Volume: %3f16" + calc1.sphereVolume());` will do the exact same thing as `System.out.printf("Sphere Volume: %3f164.188790204786385");` or something similar. I.e. the string representation of the volume becomes part of the format string. – ajb Oct 31 '14 at 17:13
  • The way my code works right now (or atleast I hope it does) is the user inputs the radius and height. The radius and height are used in formula from a method from another class in the same folder, and then I'm calling to them when I am printing. I seem to be getting the same error. Do I need to do anything to connect my arguments to my other class other than call the method I want to use? – getNoob.java Oct 31 '14 at 17:16

1 Answers1

1

The System.out.printf() method has the following signature (String, Object...) (according to the JavaDoc).

Now lets go to your code:

System.out.printf("Sphere Volume: %3f16" + calc1.sphereVolume());

The + sign concatenates the two values. The result will be a String because the first argument is a String. So lets assume that calc1.sphereVolume() returns 1.0. The VM will convert the 1.0 float value to String and you will get the newly create "Sphere Volume: %3f161.00000000000000" String. When the JVM tries to execute the printf method it sees that there is a placeholder (the "%3f16") and it searched in the varagrs (the Objec... parameter). It sees that there is no varargs an throws an exception. So basically this is what happens :)

Hope this was helpful :)

Kiril Aleksandrov
  • 2,601
  • 20
  • 27