I'm working on a school project, beginner stuff. I need to output the data with 2 decimals. I've tried several variations of

printf("%4.2f" + variable)

But it seems when I use more than one variable it craps out or wont compile. Thoughts?

public class Conference4 {
    public static void main(String[] args) {
    System.out.println("Feet\t\tMeters\tFeet\tMeters");
    System.out.println("---------------------------------------------");

    double foot = 1; double meter = 20;
    for (int i = 1; i <= 10; foot++, meter += 5, i++) {
      System.out.println(foot + "\t\t" + footToMeter(foot) + "\t|\t" + meter + "\t\t" + meterToFoot(meter));
    }
    }

    public static double footToMeter(double foot) {
        return (3.05 * foot);
    }

    public static double meterToFoot(double meter) {
        return (meter / .305);
    }
}

You mean like

System.out.printf("%4.2f\t%4.2f\n", var1, var2);

Check out the Java API for PrintStream and Formatter to see details on printf.

You could use a DecimalFormat. You'll want something like:

DecimalFormat df = new DecimalFormat("#.##");
	double test = 242.23232;
	System.out.println(df.format(test));

Edited 4 Years Ago by dmanw100: n/a

when I place that in the code I get an error that it cannot find the class DecimalFormat.

when I place that in the code I get an error that it cannot find the class DecimalFormat.

try importing

import java.text.DecimalFormat;

That relieved the class error but now I get an exception. I also am not sure on how to apply the format two the two variables that are called by the function.

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(df.format(foot) + "\t\t" + footToMeter(foot) + "\t|\t" + df.format(meter) + "\t\t" + meterToFoot(meter));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

That relieved the class error but now I get an exception. I also am not sure on how to apply the format two the two variables that are called by the function.

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(df.format(foot) + "\t\t" + footToMeter(foot) + "\t|\t" + df.format(meter) + "\t\t" + meterToFoot(meter));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

like so:

System.out.println(df.format(footToMeter(foot))+ "\t\t" + df.format(meterToFoot(meter)));

because right now your code is formating foot->System.out.println(df.format(foot)) which has no decimals, so first call the function and then use the value the function returns and format that,or combine it into a single statement as above

Edited 4 Years Ago by DavidKroukamp: n/a

This returns a "java.util.IllegalFormatConversionException: f != java.lang.String" error

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(df.format(foot) + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + df.format(meter) + "\t\t" + df.format(meterToFoot(meter)));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

This returns a "java.util.IllegalFormatConversionException: f != java.lang.String" error

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(df.format(foot) + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + df.format(meter) + "\t\t" + df.format(meterToFoot(meter)));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

uhm i dont know it works perfect for me, though i would not reccomend this:

df.format(meter)

because your foot and meter variables never have decimals, only when the get converted? but even without doing that i still have no problem
here is the code i used which is exact same:

public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("Feet\t\tMeters\tFeet\tMeters");
        System.out.println("---------------------------------------------");

        double foot = 1;
        double meter = 20;
        for (int i = 1; i <= 10; foot++, meter += 5, i++) {
            System.out.println(df.format(foot) + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + df.format(meter) + "\t\t" + df.format(meterToFoot(meter)));
        }
    }

    public static double footToMeter(double foot) {
        return (3.05 * foot);
    }

    public static double meterToFoot(double meter) {
        return (meter / .305);
    }

but what line is this error occuring on for you?

and just for having your code flawless do this:

System.out.println("Feet\t\tMeters\t\tFeet\t\tMeters");
        System.out.println("-------------------------------------------------------");

it will make your output look nice and in line :)

Edited 4 Years Ago by DavidKroukamp: n/a

That makes sense (since they will never be a decimal), But even when removed I get the error..

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(foot + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + meter + "\t\t" + df.format(meterToFoot(meter)));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

That makes sense (since they will never be a decimal), But even when removed I get the error..

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(foot + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + meter + "\t\t" + df.format(meterToFoot(meter)));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

please see my edited post.....at what line is your exception? and is that your full code? because i cant see any variable 'f' thats a string?

Edited 4 Years Ago by DavidKroukamp: n/a

Sorry, Yes this is the full code. I agree, I'm not sure what "f string" is...


It does not reveal a line. but in the output I get the first line "feet Meters, etc" and I get the "-------" line. then I get the error. So I am assuming it would be around line 12. If I remove the format the program runs. Its just UGLY.

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\t\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(foot + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + meter + "\t\t" + df.format(meterToFoot(meter)));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

Edited 4 Years Ago by jpsider: n/a

It does not reveal a line. but in the output I get the first line "feet Meters, etc" and I get the "-------" line. then I get the error. So I am assuming it would be around line 12. If I remove the format the program runs. Its just UGLY.

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

public class Conference4 {
    public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("#.##");
    	System.out.println("Feet\t\tMeters\t\tFeet\tMeters");
    	System.out.println("---------------------------------------------");
     
    	double foot = 1; double meter = 20;
    	for (int i = 1; i <= 10; foot++, meter += 5, i++) {
    		System.out.println(foot + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + meter + "\t\t" + df.format(meterToFoot(meter)));
    	}
    }
     
    public static double footToMeter(double foot) {
    	return (3.05 * foot);
    }
     
    public static double meterToFoot(double meter) {
    	return (meter / .305);
    }
}

ahh sorry man i cant help, not because i dont want to, but because my code that i copied and pasted from you isnt giving me any error and all the output is shown just fine. but if you need to error check then start by commenting out your system,out.println() method in the for statement and run your code, if it works, then add one variable like this System.out.println(foot), if that works then add the df.format(footToMeter(foot)) until you get the error again it will help to narrow the possibilities down

Edited 4 Years Ago by DavidKroukamp: n/a

Feet Meters Feet Meters
---------------------------------------------
1 3,05 | 20 65,57

i don't get any error either, but your convertions are inversed, the day im 17.08 meters tall isn't anywhere near ;)

I was trying to give you a raise!! I'll have to run this at home.. I'm working on some archaic equipment here at work...

Thanks! Stay tuned to the night version..

So you guys are right.. works just fine. not sure what was up at work... Final code with updated math.

import java.text.DecimalFormat;
     
public class Conference4 {
    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("Feet\t\tMeters\t\tMeters\t\tFeet");
        System.out.println("---------------------------------------------");
     
        double foot = 1; double meter = 20;
        for (int i = 1; i <= 10; foot++, meter += 5, i++) {
            System.out.println(foot + "\t\t" + df.format(footToMeter(foot)) + "\t|\t" + meter + "\t\t" + df.format(meterToFoot(meter)));
        }
    }
     
    public static double footToMeter(double foot) {
        return (foot / 3.05);
    }
     
    public static double meterToFoot(double meter) {
        return (meter / .305);
    }
}
This question has already been answered. Start a new discussion instead.