## ProgrammingJava Programming Question (help needed) (Convert2Decimal)Posted: Mon Oct 08, 2018 9:45 am

Cyberking
• Christmas!
Status: Offline
Joined: Oct 01, 20117 Year Member
Posts: 516
Reputation Power: 26
 I'm working on this project on netbeans to create a java program that calculates the speed of sound. The project i created works and displays the code properly but i need it to display the time in seconds to 2nd decimal place. That's where the problem lies. Whenever I try to convert it from double to decimal I get an error. If you can help me it would be much appreciated!!! CODE: ```package thespeedofsound; import java.util.Scanner;  // @author 01808415 /*  medium      speed     (air)-------(1100)     (water)-----(4900)     (steel)-----(16,400) */ public class TheSpeedOfSound { public static void main(String[] args) {      Scanner keyboard = new Scanner(System.in);    double time;  double distance;      System.out.print("Enter air, water, or steel: ");     String input;     input = keyboard.nextLine();  System.out.print("Enter distance: ");     distance = keyboard.nextDouble();         //the amount of time it takes sound to travel in air     if (input.equals("air")){time = (distance / 1100);     System.out.println("The total amount of time traveled is " + time + ".");}         //the amount of time it takes sound to travel in water     else if (input.equals("water")){time = (distance / 4900);     System.out.println("The total amount of time traveled is " + time + ".");}         //the amount of time it takes sound to travel in steel     else if (input.equals("steel")){time = (distance / 16400);     System.out.println("The total amount of time traveled is " + time + ".");}                     }     }         ``` [ Register or Signin to view external links. ]
#2. Posted:
Gavin-
• Blind Luck
Status: Offline
Joined: Nov 02, 20135 Year Member
Posts: 4,326
Reputation Power: 1846
 Hope the below helps, I have added in a numberformatter. [ Register or Signin to view external links. ]
#3. Posted:
Cyberking
• Winter 2016
Status: Offline
Joined: Oct 01, 20117 Year Member
Posts: 516
Reputation Power: 26
 Thank you this works Gavin- wroteHope the below helps, I have added in a numberformatter. [ Register or Signin to view external links. ]
Users browsing this topic: None