Results 1 to 5 of 5
- 06-23-2008, 12:09 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 7
- Rep Power
- 0
Why my program cannot calculate the decimal value?
Hi, newbie here just learning to get use of java.
I wan my program to calculate an output when user keyin an input number and round the output number to 7 decimal places.
The formula I use below in my program:
y=(x*1.5/2048)-0.80
My program can be compiled with no error.
However, when I keyin an input number, it cannot be calculated.
For example, when I keyin a number "1107" or any other decimal values like (1107.07)
It shows the errors below:
The program is below,Enter input number: 1107
Output is Exception in thread "main" java.util.IllegalFormatConversionException:
d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:399
2)
at java.util.Formatter$FormatSpecifier.printInteger(F ormatter.java:2708)
at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2660)
at java.util.Formatter.format(Formatter.java:2432)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at MyFormula.main(MyFormula.java:22)
What's wrong actually? Hope I could find an expert here to guide. Thank you.Java Code:import java.util.Scanner; public class MyFormula { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); double input_num; double output_num; System.out.print( "Enter input number: " ); input_num = input.nextInt(); output_num = (input_num*1.5/2048)-0.80; System.out.printf( "Output is %d\n", output_num ); } }
- 06-23-2008, 12:15 AM #2
You are calling next Integer when the datatype is double.
while(input. hasNextFloat()){
//......
- 06-23-2008, 12:23 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 7
- Rep Power
- 0
I am having trouble with input.nextInt(). Where should I correct my code?
-
For cripes sake, there's only one place you're calling nextInt. Can't you figure that out?
- 06-23-2008, 12:52 AM #5
Java Code:Scanner input = new Scanner( System.in ); double input_num; // declaration double output_num; System.out.print( "Enter input number: " ); // "input_num" is declared as type [i]double[/i] // so you will have trouble if you try to read // a [i]double[/i] with the [i]nextInt[/i] method. Use // [i]nextDouble[/i] input_num = input.nextDouble(); output_num = (input_num*1.5/2048)-0.80; // You want to print type [i]double[/i] here // so you must specify a floating point format (%f) // instead of integer format (%d). System.out.printf( "Output is %.3f\n", output_num );
Similar Threads
-
round to two decimal places
By javaMike in forum New To JavaReplies: 3Last Post: 12-24-2011, 02:01 AM -
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM -
Calculate Tax in java
By toby in forum New To JavaReplies: 2Last Post: 07-30-2007, 09:03 AM -
Calculate what e1 and e2 should be
By Legoland in forum New To JavaReplies: 11Last Post: 07-02-2007, 06:01 PM -
Calculate average age for women and men?
By Legoland in forum New To JavaReplies: 3Last Post: 04-18-2007, 10:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks