Results 1 to 5 of 5
  1. #1
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default 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:
    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)
    The program is below,
    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 ); 
    
       } 
    
    }
    What's wrong actually? Hope I could find an expert here to guide. Thank you.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    You are calling next Integer when the datatype is double.

    while(input. hasNextFloat()){
    //......

  3. #3
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    I am having trouble with input.nextInt(). Where should I correct my code?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For cripes sake, there's only one place you're calling nextInt. Can't you figure that out?

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM
  3. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:03 AM
  4. Calculate what e1 and e2 should be
    By Legoland in forum New To Java
    Replies: 11
    Last Post: 07-02-2007, 06:01 PM
  5. Calculate average age for women and men?
    By Legoland in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 10:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •