please help!!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: please help!!!

  1. #1
    Join Date
    Oct 2004
    Posts
    3

    please help!!!

    I am compiling a tax program and I have this compiler problem that I cant figure out . Can some one please help me. java:28: possible loss of precision
    found : double
    required: int
    StateTaxDue = (0.03 * incomeValue - (600 * dependentsValue));

    Heres my program:

    import java.io.*;
    import javax.swing.JOptionPane;
    public class StateTax
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader inData;
    inData = new BufferedReader(new InputStreamReader(System.in));

    //declaring Variables
    String income, dependents;
    int StateTaxDue;
    int incomeValue = Integer.parseInt(income);
    int dependentsValue = Integer.parseInt(dependents);

    //user input
    System.out.println("State Tax Computation");
    income = JOptionPane.showInputDialog(null, "Taxpayers Income:");
    dependents = JOptionPane.showInputDialog(null, "Number Of Dependents:");

    //Conversions
    StateTaxDue = (0.03 * incomeValue - (600 * dependentsValue));

    //output
    JOptionPane.showMessageDialog(null, "State Tax Due:" + StateTaxDue);
    System.exit(0);


    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    this is because an int cannot handle decimal points, you need to use a double for the dependants and dependantsValue variable.
    The compiler is telling you that you will loose the precision of your calculation if you use an int instead of double
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    When you cast a double to an int the JVM simply takes all the information held in the decimal points and gets rid of it. If you have the number 5.7 and you turn it into an int, you get 5. Any conversion that requires losing information like this need to be done explicitly (by casting). If you're going the other way, from an int to a double, then the JVM will do that automatically for you because no information has been lost.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center