java.util.IllegalFormatConversionException error
I have written the following code for a java class. I am currently getting the following error. Any ideas as to what is causing this. I am guessing after I specify a format I cant add more information to the print line. For an easier method i originally made it into 2 print statements, but wanted to know if it would work in one. This is in reference to lines 16 and 17.
Thanks,
Code:
Feet Meters Meters Feet
1.0 Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at ConversionBetweenFeetAndMeters.main(ConversionBetweenFeetAndMeters.java:12)
Code:
public class ConversionBetweenFeetAndMeters {
public static void main(String[] args) {
//Assign Variables
double meter = 0, foot = 0, footCount, meterCount;
// Display Header
System.out.println("Feet\tMeters\tMeters\tFeet");
// Create Loop
for (footCount = 1, meterCount = 20; footCount <= 10
&& meterCount <= 65; footCount++, meterCount = meterCount + 5) {
foot = meterToFoot(meterCount);
meter = footToMeter(footCount);
System.out.printf("\n" + footCount + "\t" + "%.3f", meter + "\t" + meterCount + "\t" + "%.3f", foot);
//System.out.printf("\t" + meterCount + "\t" + "%.3f", foot);
}
}
// Calculate Feet To Meters
public static double footToMeter(double foot) {
return 0.305 * foot;
}
// Calculate Meters To Feet
public static double meterToFoot(double meter) {
return meter / 0.305;
}
}
Re: java.util.IllegalFormatConversionException error
Look at the API doc for the printf() method. I don't think you are using it correctly.
If you have questions about what the doc says, copy the doc here with your questions.
Re: java.util.IllegalFormatConversionException error
What the code is supposed to do is .
Code:
Feet Meters Meters Feet
1.0 0.305 20.0 65.574
2.0 0.610 25.0 81.967
3.0 0.915 30.0 98.361
4.0 1.220 35.0 114.754
5.0 1.525 40.0 131.148
6.0 1.830 45.0 147.541
7.0 2.135 50.0 163.934
8.0 2.440 55.0 180.328
9.0 2.745 60.0 196.721
10.0 3.050 65.0 213.115
This code acheives that affect, but I was just hoping lines 21 and 22 could be combined somehow to take one less line.
Code:
public class ConversionBetweenFeetAndMeters {
public static void main(String[] args) {
// Assign Variables
double meter = 0, foot = 0, footCount, meterCount;
// Display Header
System.out.println("Feet\tMeters\tMeters\tFeet");
// Create Loop
for (footCount = 1, meterCount = 20; footCount <= 10
&& meterCount <= 65; footCount++, meterCount = meterCount + 5) {
// Receive returned formulas from Methods and calculate
foot = meterToFoot(meterCount);
meter = footToMeter(footCount);
System.out.printf("\n" + footCount + "\t" + "%.3f", meter);
System.out.printf("\t" + meterCount + "\t" + "%.3f", foot);
}
}
// Calculate Feet To Meters
public static double footToMeter(double foot) {
return 0.305 * foot;
}
// Calculate Meters To Feet
public static double meterToFoot(double meter) {
return meter / 0.305;
}
}
Re: java.util.IllegalFormatConversionException error
Quote:
somehow to take one less line.
That is absolutely not important. It's Not worth the effort.
Did you read the API doc for the printf method to see what was wrong with your previous code?
Re: java.util.IllegalFormatConversionException error
Were can I find the API Doc. I am using eclipse.
Re: java.util.IllegalFormatConversionException error
Go to this site and Find the name of the class in the lower left.
The doc for the class will show in the main window.
Scroll down to the method you want to read about.
Java Platform SE 6
Re: java.util.IllegalFormatConversionException error
Thanks for the help. Just a beginner here. Think they would mention that website somewhere in the college course book.
Re: java.util.IllegalFormatConversionException error
That website is "THE WEB SITE" for java. You should save the API link. I use it dozens of times a day.
Re: java.util.IllegalFormatConversionException error
Got it bookmarked. Just want to say thank you again. I have gotten to where i can write the basics of java code for a program pretty quickly. Now I am trying to learn how to clean it up a bit more each class. Before today I actually had to "for" loops because I did not know you can have a for loop with multiple conditions.
It realyl is sites like this and helpful people that really get people to stay working on a programming language. If you were stuck to a college course a lot of people would get burnt out and give up completely.
Re: java.util.IllegalFormatConversionException error
Keep writing code and getting it to work. That's how you learn.