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!!
–
–