Results 1 to 7 of 7

Thread: Advice please

  1. #1
    Martin_smith is offline Member
    Join Date
    Sep 2013
    Location
    Uruguay
    Posts
    3
    Rep Power
    0

    Default Advice please

    Hello everyone, I am new to this forum. I have recently begun programming Java, I am studying Systems Analyst and I don't get to see Java until my 4th semester, but I like Java so I have decided to start learning it by myself through tutorials and different books.
    I wrote a simple calculator but I am getting this error, maybe some of you out there can point out the reason why I am getting this error.
    package javacourse;
    import java.util.Scanner;

    public class apples {
    public static void main(String args[]){
    Scanner calculate = new Scanner(System.in);
    double fnum, snum, answer;
    System.out.println("Enter your first integer: ");
    fnum = calculate.nextDouble();
    snum = calculate.nextDouble();
    System.out.println("Enter your second integer: ");
    answer = fnum + snum;
    System.out.println(answer);
    }




    }


    When I add or subtract whole numbers I don't have any sort of issues, now when I add a decimal point I get the following error
    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.nextDouble(Unknown Source)
    at javacourse.apples.main(apples.java:9)



    if anyone could give me a hand on pointing out the mistake that would be great, thanks a lot

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Advice please

    I doubt this works with either integers or doubles.

    Compare what the next() functions do to what the nextLine() function does. Hint: the enter key counts as a character.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Martin_smith is offline Member
    Join Date
    Sep 2013
    Location
    Uruguay
    Posts
    3
    Rep Power
    0

    Default Re: Advice please

    I tried to tweek around what you said but I am still getting the same thing...sorry I am a complete beginner haha

    So what is your best advice to build a calculator with the functions I have on my code?

    oh by the way how can I properly copy and paste my code on a thread so it looks better for a future post?

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Advice please

    Hmm, it seems to work perfectly fine for me?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Advice please

    You use the code tags. It shouldn't be too hard to find how to use them (the forum search is your friend).

    Why are you reading snum before you ask for it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Martin_smith is offline Member
    Join Date
    Sep 2013
    Location
    Uruguay
    Posts
    3
    Rep Power
    0

    Default Re: Advice please

    yeah I made that error copying and pasting
    package javacourse;
    import java.util.Scanner;

    public class apples {
    public static void main(String args[]){
    Scanner calculate = new Scanner(System.in);
    double fnum, snum, answer;
    System.out.println("Enter your first integer: ");
    fnum = calculate.nextInt();
    System.out.println("Enter your second integer: ");
    snum = calculate.nextInt();
    answer = fnum + snum;
    System.out.println(answer);
    }




    }

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: Advice please

    You're using the nextInt() function. That function throws an Exception if the user enters anything other than an int. There are other functions that you might want to use instead.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. hi need some advice
    By addrianbfr in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 05-10-2012, 09:39 AM
  2. advice
    By icedust in forum Advanced Java
    Replies: 1
    Last Post: 10-11-2011, 12:50 AM
  3. Advice
    By tabchas in forum New To Java
    Replies: 1
    Last Post: 07-25-2011, 09:59 AM
  4. Im new n looking for an advice
    By azlynn in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 03:47 AM
  5. Some advice please!
    By awebbtt in forum New To Java
    Replies: 3
    Last Post: 02-02-2009, 08:23 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
  •