Results 1 to 10 of 10
  1. #1
    cartotech81 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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,



    Java 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)
    Java 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;
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  3. #3
    cartotech81 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: java.util.IllegalFormatConversionException error

    What the code is supposed to do is .

    Java 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.

    Java 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;
    	}
    }
    Last edited by cartotech81; 11-29-2011 at 09:27 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: java.util.IllegalFormatConversionException error

    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?

  5. #5
    cartotech81 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: java.util.IllegalFormatConversionException error

    Were can I find the API Doc. I am using eclipse.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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

  7. #7
    cartotech81 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.

  9. #9
    cartotech81 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: java.util.IllegalFormatConversionException error

    Keep writing code and getting it to work. That's how you learn.

Similar Threads

  1. Replies: 2
    Last Post: 03-09-2010, 08:18 AM
  2. Jre upgrade Issue :java.util.zip.ZipException: error in opening zip file
    By selvakumar.velmurugesan in forum New To Java
    Replies: 0
    Last Post: 10-27-2009, 05:20 AM
  3. Replies: 0
    Last Post: 03-30-2009, 07:35 PM
  4. java.util.UnknownFormatConversionException error
    By rosh72851 in forum Advanced Java
    Replies: 7
    Last Post: 10-09-2008, 04:15 AM
  5. java.util
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 02-07-2008, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •