Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams String.Format for rounding, can't locate illegal format conversion source error? (1 answer)
Closed 2 years ago .

I'm trying to create a simple java program that reads a floating-point number, calculates it using an equation with Euler's number then converts the results from double to int. I can get it to compile without errors but after I input the floating-point numbers I get the error:

nB = Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
    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 Ex4.main(Ex4.java:16)

Its only a few lines of coding and I just created a similar program that works fine, so I don't understand what's wrong here! I'm thinking it might have to do with the line on Euler's number since I don't fully understand all the math functions yet, but I've used π just fine before the exact same way. Or is it because the final conversion step is wrong? Sorry if it's something super obvious, i've tried my best to debug it so many times but any time I change a part I think could be the issue I end up with so many more errors. Here is the code:

import static java.lang.Math.*;
import java.util.Scanner;
class Ex4
    public static void main( String [ ] args)
        Scanner input = new Scanner(System.in);         //import scanner and ask user to input number
        System.out.print("Please enter a floating-point number: ");
        double nA = input.nextDouble();     
        double nB = Math.pow(E, nA);     //mathematical equation
        int value = (int) nB;    //convert nB to an int
        System.out.printf("nB = %f", value);

Pitifully small and simple, I know ;_; I also tried looking up similar questions on here but all the errors any commenters found I triple checked were right in mine. Also if anyone could help me understand the error message better as well, i'd really appreciate it. Thank you!!

A couple of suggestions: Slow down, read the error message carefully, and don't fall into the trap of taking shots in the dark making random adjustments you don't understand. To start with, you don't have to guess which line of code is at fault. The error message has the exact file and line number, Ex4.java:16. Then there's the error message, java.util.IllegalFormatConversionException: f != java.lang.Integer. What do you make of that? (If it means nothing to you, Google it and see what turns up.) – John Kugelman Apr 2, 2021 at 0:00 Thank you for the perfectly detailed answer it really helped, I guess I've just been in a dizzy working on different code all morning and didn't realize that the error pointed out the specific spot. I'll keep that in mind for future programs and try to work on slowly and carefully going through what I've written before getting too frustrated haha. Thank you again!! – Rosie Apr 2, 2021 at 0:31