The problem states to write a method named fallingDistance that accepts an object's falling time as an argument. The method should return the distance that the object has fallen during the time interval. Demonstrate the method by calling it in a loop that passes the values 1-10 as arguments and displays the return value......

I got it to compile but get a hole bunch of this:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3992)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2708)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2660)
at java.util.Formatter.format(Formatter.java:2432)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at fallingDistances1.fallingDistance(fallingDistances1.java:37)
at fallingDistances1.main(fallingDistances1.java:17)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Help what am i doing wrong?

import java.util.*;
import java.io.*;
import java.text.DecimalFormat;

public class fallingDistances1
{
	public static void main (String [] args) throws IOException
	{
	
	System.out.println("Time\tDistance");
	System.out.println("-----------------");

	fallingDistance();
	}
	// method
	public static void fallingDistance ()
	{
	//constants
	final double  starting_time = 1.0;
	final double  max_time = 10.0;
	final double  increment = 1.0;
	// variables
	double distance;
	double g = 9.8;
	double t = 0;
	double a = 0.5;
	
	for (distance = starting_time; distance <= max_time; distance =+ increment)
		{
			//calculate distance
			distance = (a)*(g)*(t*t);
			
			System.out.printf("%d\t\t%.1f\n",t, distance);	
			
			System.exit(0);		

		}
	}
}

Recommended Answers

All 4 Replies

In your printf statement, you use %d which is used for integers, but you associate with that the variable t, which is a double. The error message is saying that %d cannot be used with a double.

You need to replace the %d with %f to print float numbers.

System.out.printf("%f\t\t%.1f\n",t, distance);

awesome that fixed the error message i got when i ran the program but now it only shows the time as being 0 and the distance being 0....its not calculating hmmmm

haha nevermind wow i feel like an airhead i put distance instead of time in my for loop oops

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.