2

This is the value of the double "m" in my program (the mass of a planet after calculations, this value is specifically for the mass of Earth)

  • 5.973405437304745E24

When printing using System.out.println(m); The output is

  • 5.973405437304745E24 (correct output)

When printing using System.out.println(Math.round(m)); The output is

  • 9223372036854775807 (incorrect output)

How am I able to shorten the value of m so it fits within %6s? Like this for example

  • 5.97E24

This is my code below. The assignment requires us to output the final values in a formatted chart (as I've attempted to do below)

//Java imports
import java.util.*;
import java.lang.Math;

//Main class
class Main {
  public static void main(String[] args) {
    //Initializing scanner name userInput
    Scanner userInput = new Scanner (System.in);

    //Greeting statement, prompts user to input the circumference in km
    System.out.println("\nWelcome to the Escape Velocity Application. To begin, please enter the following information below. \nEnter the circumference (km):");

    //Stores circumference in double named "circum" and converts the value to its meter equivalent (unit conversion required)
    double circum = userInput.nextDouble() * Math.pow(10, 3); 

    //Prompts user to input the acceleration in m/s^2
    System.out.println("Enter the acceleration due to gravity (m/s^2):");

    //Stored value in double named "f"
    double f = userInput.nextDouble(); 

    //Gravitational Constant
    double G = 6.67408e-11;

    //1 - Radius calculation using the circumference of a circle formula
    double r = circum/(2*Math.PI);

    //2 - Mass calculation using the gravity formula
    double m = f*Math.pow(r, 2)/G;

    //3 - Calculation escape velocity using the escape velocity formula
    double e = (Math.sqrt((2.0*G*(m))/r));

    //Final output statements
    System.out.println("\nThe radius is: " + Math.round(r * Math.pow(10, -3)) + " kilometers.");
    System.out.println("The mass is: " + m + " kg.");
    System.out.println("The escape velocity is: " + Math.round(e) + " m/s.");

    //Formatted output statements
    System.out.format("\n%20s %6s %10s", "Radius:", Math.round(r * Math.pow(10, -3)), "km.");
    System.out.format("\n%20s %6s %10s", "Mass:", Math.round(m), "kg.");
    System.out.format("\n%20s %6s %10s", "Escape Velocity:", Math.round(e), "m/s.");
  }
}

This is what the output looks like. The center of the second line is offset due to the long value of m.

The radius is: 6378 kilometers.
The mass is: 5.973405437304745E24 kg.
The escape velocity is: 11181 m/s.

         Radius:   6378        km.
           Mass: 9223372036854775807        kg.
Escape Velocity:  11181       m/s.
BipoN
  • 93
  • 8

1 Answers1

4

You could use the following code:

double x = 5.973405437304745e24;
System.out.printf("Mass: %.2e kg.%n", x);

Which outputs Mass: 5.97e+24 kg..

%.2e formats the number and %n just adds a newline character. The .2 specifies that two decimal places after the dot are desired. The e requests scientific notation from the formatter.

The problem with Math.round() is, that the result is stored in a long which cannot represent such a large number.

Jannik
  • 1,583
  • 1
  • 14
  • 22
  • Ah I see, thank you for your feedback. Using your code, the output is much shorter but it seems that the next part of the statement is printed on a new line. Is there a way to fix this? – BipoN Oct 14 '19 at 21:35
  • System.out.format("\n%20s %.2e%n %20s", "Mass:", m, "kg."); – BipoN Oct 14 '19 at 21:36
  • That is the line I am using, the "kg" somehow is printed on a new line. – BipoN Oct 14 '19 at 21:37
  • Get rid of the `%n`, that's a new line. Or, it might be your console not being wide enough :o – Avi Oct 14 '19 at 21:37
  • Thanks boss! Worked perfectly. – BipoN Oct 14 '19 at 23:52