You are viewing our Forum Archives. To view or take place in current topics click here.
Java Programming Question (help needed) (Convert2Decimal)
Posted:

Java Programming Question (help needed) (Convert2Decimal)Posted:

Cyberking
  • Winter 2016
Status: Offline
Joined: Oct 01, 201112Year Member
Posts: 532
Reputation Power: 28
Status: Offline
Joined: Oct 01, 201112Year Member
Posts: 532
Reputation Power: 28
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-
  • Winter 2020
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Hope the below helps, I have added in a numberformatter.

[ Register or Signin to view external links. ]
#3. Posted:
Cyberking
  • Wise One
Status: Offline
Joined: Oct 01, 201112Year Member
Posts: 532
Reputation Power: 28
Status: Offline
Joined: Oct 01, 201112Year Member
Posts: 532
Reputation Power: 28
Thank you this works

Gavin- wrote Hope the below helps, I have added in a numberformatter.

[ Register or Signin to view external links. ]
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.