Hey guys! I have a program I am writing that I have gotten stuck on. It compiles fine but when I run it I get several error messages. Illegal format Conversion Exception is one of the error messages. I am stuck and I don't see where the error is. Any insight or guidance would be greatly appreciated! Thank you all in advance!

import java.util.Scanner;

public class Circles
{
	
	double radius = 1;
	
	
	public Circles( double rad )
	{
		radius = rad;
				
	}
	
	public void setRadius( double rad)
	{
		radius = rad;
	}
	
	public Double getRadius()
	{
		return radius;
	}
	
	public void displayMessage()
	{
		System.out.printf( "The radius of the circle is \n%d!\n", getRadius() );
		
	}
    
	public void determinePerimeter()
	{
		double perimeter;
		
		perimeter = 2 * getRadius() * 3.1415;
		
		System.out.printf( "The perimeter is: %d!\n", perimeter );
	}
	
	public void determineArea()
	{
		double area;
		
		area = getRadius() * getRadius() * 3.1415;
		
		System.out.printf( "The area is: %d!\n", area );
	}





}
import java.util.Scanner;	
	public class CirclesTest
	{
	
	
		public static void main( String[] args )
		{
		
		Circles myCircles = new Circles( 1.00 );
		
		
		
		   Scanner input = new Scanner( System.in );
		
		   System.out.print( "Please enter radius of circle:");
		   myCircles.radius = input.nextDouble();
		
		
		
		
		   myCircles.displayMessage();
		   myCircles.determinePerimeter();
		   myCircles.determineArea();
		
		}
		
	}

Recommended Answers

All 6 Replies

Post your error messages.

Also you have a method: setRadius
Why don't you use that instead of this: myCircles.radius = input.nextDouble()
What's the point of having it?

I believe the set and get methods are used for code reusability...?

This is what happens when I try to run the program.

Please enter radius of circle:4
The radius of the circle is
Exception in thread "main" java.util.IllegalFormatConversionException: d != java
.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:399
2)
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 Circles.displayMessage(Circles.java:29)
at CirclesTest.main(CirclesTest.java:23)
Press any key to continue...

I believe the set and get methods are used for code reusability...?

This is what happens when I try to run the program.

Please enter radius of circle:4
The radius of the circle is
Exception in thread "main" java.util.IllegalFormatConversionException: d != java
.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:399
2)
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 Circles.displayMessage(Circles.java:29)
at CirclesTest.main(CirclesTest.java:23)
Press any key to continue...

The stack trace tells you exactly where the error is. It would be helpful next time to read it and if you can't fix it, to point where that line is at the code you posted.

Exception in thread "main" java.util.IllegalFormatConversionException: d != java
.lang.Double

at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:399
2)
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 Circles.displayMessage(Circles.java:29)
at CirclesTest.main(CirclesTest.java:23)

The error is at the displayMessage method of the Circles.java file at line 29.

The getRadius method returns Double, which is an object. The printf method: "The radius of the circle is \n%d!\n"
expects a double primitive type.

So I think that if you change the return type of the getRadius method to double should correct your problem.
It is better, when you create get/set methods, to have the argument of the "set" and the return type of the "get" to be exactly the same type as the variable used.

Thank you very much! That was the problem!

I believe the set and get methods are used for code reusability...?

Not particularly. They're more useful for encapsulation - hiding your data and only making their use available through methods that you (the programmer) implement. For example, you might want to encapsulate a data member "hours" and provide a set method that only allows a user of your class to set hours to an integer from 1-12. After all, you probably don't want the hour to be anything else.

That is a good point! Thank you for clarifying! I remember reading that now that you said it.

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.