0
public class TryMyMath {

    public static void main(String args[])
    {

        double num = Math.E;
        printTable(num);
    }// end main

    public static void printTable(double num){
        int n = 1;

        for(n = 1;n <= 10; n++)
        {
            num = Math.pow(num, n);
            System.out.printf("%d %lf", n, num);
        }
        round100th(num);

    }
    public static double round100th(double num)
    {
        return Math.round(num*100.)/100.0 ;
    } // end round100th

}

i have this question with using printf in java, so this is the error i get with printf

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'l'
  at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
  at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
  at java.util.Formatter.parse(Unknown Source)
  at java.util.Formatter.format(Unknown Source)
  at java.io.PrintStream.format(Unknown Source)
  at java.io.PrintStream.printf(Unknown Source)
  at TryMyMath.printTable(TryMyMath.java:15)
  at TryMyMath.main(TryMyMath.java:7)

when i take out printf statement there wasn't any error, am I not using printf the right way it is supposed to be used in java?

and also i'm new to java and eclipse so i don't know what those error mean.

so this is the out put that i get

1 2.7182822 7.3890563 403.4287934 26489122129.8434375 13041808783936237000000000000000000000000000000000000.0000006 Infinity7 Infinity8 Infinity9 Infinity10 Infinity

the output is only right up to n = 3 i don't see anything wrong with the for loop why is this happen?

nvm i saw what wrong with it

1
  • 1
    You come from C? "%f" works for double in Java Commented Oct 4, 2012 at 20:05

5 Answers 5

1

It should be System.out.printf("%d %f", n, num). The l is meaningless here.

Additionally, your round100th(num); call has no effect - are you trying to print the numbers to two decimal places? You can do that with:

System.out.printf("%d %.2f", n, num)
Sign up to request clarification or add additional context in comments.

Comments

1

See the String Formatter Conversions, there is no %l.

Comments

0

I'm not positive about this, but I believe in java you only have to use

System.out.printf(n + " " + num)

It's been awhile though, so you'll need to check it. Please tell me if I'm wrong. I don't want to leave an incorrect answer up.

Comments

0

Try:

System.out.printf("%d %f", n, num);

The formatting is described in great in JavaDocs.

Comments

0

The problem is with this line:

System.out.printf("%d %lf", n, num);

%lf is not appropriate. Did you mean %1f? Notice that the former has a lowercase "L" and the latter has a "1". Try it with a 1 and it should work.

2 Comments

"The problem is with this line:" - congratulations, you can read a stack trace - TryMyMath.java:15
oh i was thinking in c where you use %lf for double

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.