When I run my program I get this:
run:
Planetary DataPlanet Diameter(km) Mass(kg) g(m/s^2)
_________________________________________________
Exception in thread "main" java.util.IllegalFormatPrecisionException: 1
Mercury at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2936)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2684)
at java.util.Formatter.parse(Formatter.java:2528)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at GravityV1.diameterr(GravityV1.java:45)
at GravityV1.main(GravityV1.java:73)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
I am trying to display information made from static methods in the main method. Somehow, it is not displaying it when I call it in the main method, I am also trying to calculate the gravity and it isn't recognizing some of my variables like the universal constant for gravity, I made the gravity static method above the main method and declared and initialized the universal gravity constant in the main method for calculation. Any help will be greatly appreciated. Below is my code:
public class GravityV1 {
// title static method
public static void title(){
// title
System.out.printf("%10s", "Planetary Data");
// heading
System.out.println("Planet Diameter(km) Mass(kg) g(m/s^2)");
// create line
System.out.println("_________________________________________________");
}
// months static method
public static void planetss(){
// create months array
String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune"};
// display planets
System.out.printf("%5s", (Object[]) planets);
}
// diameter static method
public static void diameterr(){
// create array of diameter
double[] diameter = {4880, 12103.6, 12756, 6794, 142984, 120536, 51118,
49532};
// display diameter
System.out.printf("%4.1d", diameter);
}
// mass static method
public static void masss(){
// create array of mass
double[] mass = {3.30E23, 4.869E24, 5.97E24, 6.4219E23, 1.900E27,
5.68E26, 8.683E25, 1.0247E26};
// display mass
System.out.printf("%4.1d", mass);
}
// gravity calculation static method
public static double gravity(double g){
// calculate gravity
g = (G * mass) / Math.pow(diameter / 2, 2);
return g;
}
// main method
public static void main(String args[]){
title();
planetss();
diameterr();
masss();
double G = 6.67384 * Math.pow(10, -11);
}
}
Format specifier for double
type is not d
but f
. You should replace the printf
statement in diameterr
method to:
System.out.printf("%4.1f", diameter);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.