"Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%.4f'" - java

I'm looking to print the surface area and volume in 4 decimal places using %.4f. I have no clue what the error in the title means. Here is the source code:
import java.util.Scanner;
public class VolumeAndSurfaceArea {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Radius is abbrebriated to "r"
double r = sc.nextDouble();
double SA = 4.0 * 3.14 * r * r;
double V = 4/3 * 3.14 * r * r * r;
double helper_SA = SA;
System.out.printf("The Surface Area is: %.4f" + SA);
System.out.printf("The Volume is: %.4f" + V);
}
}
Any help would be appreciated. Thanks!

Rather than concatenating the double values to the end of your String messages, add them to the argument list like so:
System.out.printf("The Surface Area is: %.4f", SA);
System.out.printf("The Volume is: %.4f", V);

Related

cannot convert from float to double?

The formula to calculate the area of a circumference is defined as x =
π . R2. Considering to this problem that π = 3.14159:
Calculate the area using the formula given in the problem description.
Input The input contains a value of floating point (double precision),
that is the variable R.
And for an input of 2, I should be getting x=12.5664 rounded by one number.
I tried using this simple code, but I couldn't remember what to do with the "cannot convert from double to float" error. It's been half a year since I coded.
package TEST;
import java.util.Scanner;
public class TEST {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// let A be radius
float A = scanner.nextFloat();
float A_2 = A * A;
// let B be Pi
double B = 3.14159;
// let x be circumference
float x = A_2 * B;
System.out.printf("x= %.4f" + x);
}}
The cause of the compilation error is the following assignment:
float x = A_2 * B;
where B is of type, double and therefore the result of the product will of type, double which can not be accommodated into a variable of type, float. Remember: double requires 8 bytes of space whereas a float variable can accommodate only 4 bytes.
After correcting this compilation error, you will encounter a runtime error because you have used a plus sign (+) instead of a comma (,) inside the printf statement.
Apart from this,
Always follow Java naming conventions e.g. A should be a and A_2 should be a2 following the conventions.
You can use Math.PI instead of using your own value for PI.
The following code incorporates these changes:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// let a be radius
float a = scanner.nextFloat();
float a2 = a * a;
// let b be PI
double b = Math.PI;
// let x be circumference
double x = a2 * b;
System.out.printf("x= %.4f", x);
}
}
A sample run:
2
x= 12.5664
You could try:
double A_2Converted = (double) A_2;
and use that.
source: Convert float to double without losing precision

Java :calculate the circle diameter and

this is a Class about calculating diameter,circumference and area of circle that user enter radius value and it gives him diameter,cirucumf... ,
this is the class code:
package circle;
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius=0;
int diameter;
int circumference ;
int area;
int Pi;
Pi=(int) 3.14;
area = (int) (radius*radius*Pi);
circumference =(int)(radius*2*Pi);
diameter = (int)(radius*2);
System.out.print("Enter radius value:");
radius=input.nextInt();
System.out.printf("area is %d%n" , area);
System.out.printf("diameter is %d%n", diameter);
System.out.printf("circumference is %d%n", environment);
}
}
this is what output gives me :
Enter radius value: (for exmaple) 4
area is 0 // (real value is 50.24)
diameter is 0 // (8)
circumference is 0 //(25.12)
what is the code problem?
or how can i fix it?
You read the radius AFTER computing area/environment(?)/diameter. Furthermore, your values are int variables, which also means that your value for pi is just 3. I suggest you correct the order of the statements, and start using double instead of int.
"environment" will be replaced by "circumference". As your excepted output is decimal value. So use float/double instead of int. In your program you are calculating diameter,circumference and area after initialising the value of radius(radius=0) but before getting the value of radius(radius=4). I have modified your code. It seem help you.
package circle;
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius=0;
float diameter;
double circumference ;
double area;
double Pi;
Pi= 3.14;
System.out.print("Enter radius value:");
radius=input.nextInt();
area = (radius*radius*Pi);
circumference =(radius*2*Pi);
diameter = (radius*2);
System.out.printf("area is " + area);
System.out.printf("\ndiameter is "+ diameter);
System.out.printf("\ncircumference is "+ circumference);
}
}
you are calculating the values area/environment(?)/diameter before getting and initializing the radius input.And at that time the default value for radius is set which is 0. Hence it is giving the results of all the parameters as 0.So you will have to re order your code as below:
System.out.print("Enter radius value:");
radius=input.nextInt();
area = (int) (radius*radius*Pi);
environment=(int)(radius*2*Pi);
diameter = (int)(radius*2);
System.out.printf("area is %d%n" , area);
System.out.printf("diameter is %d%n", diameter);
System.out.printf("environment is %d%n", environment);

Area of a Pentagon

I was trying to write a code to take the radius of a pentagon and turn it into a side, then output the area. Somewhere along the way my use of the math class is incorrect. (New to this site but tried to format everything so you could understand my program)
Class: CS 1301/16
// Term: Fall
// Name: Gabriel Tomasetto
// Intsructor: Ms. Tulin
// Assignment 1
import java.util.*;
public class Pentagon {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double radius, side, area;
System.out.println("Please enter the length of the Pentagon from the center to the vertex: ");
radius = scan.nextDouble();
side = (2 * radius) * (Math.sin(Math.PI/5)); //I'm assuming I'm not correctly using the Math class to represent the equation? Thoughts.
area = (5 * Math.pow(radius, 2)) / (4 * Math.tan(Math.PI /5));
System.out.println("The area of the Pentagon is:\t" + area);
}
}
Shouldn't it be side = (2 * radius) * (Math.sin(Math.PI/2/5));?
What's the error you're getting? Just the wrong answer?
Just a simple tpyo I think, try this.
area = (5 * Math.pow(side, 2)) / (4 * Math.tan(Math.PI /5));
...assuming this is the right formula using side length and not radius
http://www.mathopenref.com/polygonregulararea.html

Law of Cosines used to find third side - why do I get the wrong result?

I keep getting an output of 45.8 when inputting 24 as side a, 32 as side b and 115 as angle C
the correct angle is 47.4
can someone please tell me what I'm doing wrong?
The calculation is:
double sideC = Math.sqrt((Math.pow(sideA, 2) + Math.pow(sideB, 2))- 2*(sideA*sideB)*(Math.cos(angleC)));
Source:
import java.util.Scanner;
public class TriangleCalc
{
/**
* #param args
*/
public static void main(String[] args)
{
System.out.println(" Triangle Calculator ");
Scanner inputab = new Scanner(System.in);
System.out.println("Input lenghts of sides 'a' and 'b':");
double sideA = inputab.nextDouble();
double sideB = inputab.nextDouble();
System.out.println("Input the size of Angle C in degrees:");
double angleC = inputab.nextDouble();
inputab.close();
double sideC = Math.sqrt(((Math.pow(sideA, 2) +
Math.pow(sideB, 2))- (2*(sideA*sideB)*(Math.cos(angleC)))));
System.out.println("\t /\\\n\t / \\\n\t / \\\n\t/ \\");
System.out.printf(" %3.1f",sideA);
System.out.print("/ \\");
System.out.printf("%3.1f",sideB);
System.out.println("\n / \\\n / \\\n"
+" /______________\\");
System.out.print(sideC);
As the Javadoc for Math.cos states
a - an angle, in radians.
You need to convert degrees to radians if you want to use degrees.
You can use Math.toRadians
Replace
Math.cos(angleC)
with
Math.cos(Math.toRadians(angleC))

how do I get the output answer 4 decimal places

I need to round the outputs to 4 decimal places but I am not quite sure on how to do that my thing runs exactly how I want it too just need to round off the last decimals off too 4 places
import java.util.Scanner; //Needed for the Scanner class
public class SphereCalculations
{
public static void main(String[] args) //all the action happens here!
{ Scanner input = new Scanner (System.in);
double radius;
double volume;
double surfaceArea;
System.out.println("Welcome to the Sphere Calculator. ");
System.out.print( "Enter radius of sphere: " );
radius = input.nextDouble();
volume = ((4.0 / 3.0) * (Math.PI * Math.pow(radius, 3)));
surfaceArea = 4 * (Math.PI * Math.pow(radius, 2));
System.out.println("The Results are: ");
System.out.println("Radius: " + radius);
System.out.println("Sphere volume is: " + volume);
System.out.println("Sphere Surface Area is: " + surfaceArea);
}
}
Output:
Radius: 7.5
Volume: 1767.1459
Surface Area: 706.8583
System.out.printf("Sphere Surface Area is: %.4f%n", surfaceArea);
If you find that you need more control, you can use DecimalFormat which has many more formatting options. This is especially helpful if you want to print out a double in scientific notation.
//specify a number locale, some countries use other symbols for the decimal mark
NumberFormat f = NumberFormat.getInstance(Locale.ENGLISH);
if(f instanceof DecimalFormat) {
DecimalFormat d = (DecimalFormat) f;
d.applyPattern("#.0000"); //zeros are non optional, use # instead if you don't want that
System.out.println(d.format(surfaceArea));
}

Categories

Resources