Results 1 to 4 of 4
  1. #1
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default How to subtract numbers with decimals with Java?

    Everytime I enter a number to subtract with a decimal I get an error. The code ask for a number I enter a decimal number like 5.5 and it gives me an error and I want to be able to subtract them.

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

    Default Re: How to subtract numbers with decimals with Java?

    You will want to show your code and show your error message. Else it will be quite hard to help you.

  3. #3
    coding is offline Member
    Join Date
    Jan 2011
    Posts
    40
    Rep Power
    0

    Default Re: How to subtract numbers with decimals with Java?

    public class s {
    public static void main(String[] args)
    {
    int a ;
    int b ;
    int sub;
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter purchase amount:"); //Ask user for first input.
    a=input.nextInt(); // store the input a.
    System.out.println("Please enter amount tendered :"); // Ask user for second input.
    b=input.nextInt(); // store the input in b.

    sub = a - b; //subtract the 2 two numbers

    System.out.println("Your change is : " + sub); // display the result of the two numbers

    }
    }
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at s.main(s.java:13)


    And when I enter normal numbers its fine, but when i enter decimals like 10.5 i get an error and idk how it can break up the answers in dollars, quartiers, dimes and pennies.

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

    Default Re: How to subtract numbers with decimals with Java?

    You're using your Scanner object to read in an int, since that's what it's nextInt() method logically does. If you try to enter a non-int number, it makes sense that this will cause an error. There are other Scanner methods available, but rather than just give you the solution, I would prefer inviting you to check the Scanner API, since extracting this information from the API is a useful and necessary skill if you're to program in Java. Please have a look at the method list for this class, in particular the nextXXX() series of methods, and see if one may be more appropriate for your purposes. Much luck, and come back if still stuck.

    Edit: also, when posting code in the forum, please try to wrap the code in [code] [/code] tags, not [quote] [/quote] tags as you've done above. This will make your code much easier to read.
    Last edited by Fubarable; 01-27-2013 at 01:49 AM.

Similar Threads

  1. Java does not read the decimals?
    By Josep_16 in forum New To Java
    Replies: 4
    Last Post: 08-08-2011, 09:23 AM
  2. Quick question about numbers and decimals
    By camaro01 in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 05:24 AM
  3. Formatting numbers or decimals (around comma)
    By Joris in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2010, 05:46 PM
  4. Decimals in java help!!
    By Gold in forum New To Java
    Replies: 3
    Last Post: 12-09-2009, 11:25 PM
  5. Area Subtract
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:07 PM

Posting Permissions

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