Java Programming Tutorial – 7 – Building a Basic Calculator

How to build a basic calculator with the skills we have learned so far.


(Visited 78 times, 1 visits today)

Share This Post

44 Comments - Write a Comment

  1. If you put:

    System.out.print(fnum);
    System.out.print(" + ");
    System.out.print(snum);
    System.out.print(" = ");

    between:

    answer = fnum + snum;

    and

    System.out.println(answer);

    you get a nice little calculation like:
    7.0 + 3.0 = 10.0

    but it craches by me if you use 45.3 or something waith a . and a number
    after

    Reply
  2. I follow this exactly, using the NetBeans IDe 7.4 and I just get a BUILD
    SUCCESSFUL but no ability to enter any further data. Anyone help with
    suggestions?

    Reply
  3. I copied your code there word for word (as far as I can see) but I didn't
    get the extra numbers after the decimal point.
    Any, great tutorial, as always.

    Reply
  4. Why is there a leak on line 5? its telling me "Resource leak, bucky is
    never closed" I dont know why its not closed, i have the exact same thing
    typed

    Reply
  5. why do you have to type. fnum = bucky.nextDouble(); ? can't it be just fnum
    = nextDouble(); ? because that basically says "fnum is.. wait for the
    input" ?

    Reply
  6. Your main method has a problem, try to rewrite it or just copy: public
    static void main(String[]args) and make sure that all of the instructions
    are inside the { }, you must have two like 2 } , one for you method, and
    another to close your class

    Reply
  7. Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    at Start.main(Start.java:4) I keep on getting this error message. Any help
    for this?

    Reply
  8. Tip for any beginners such as myself: 1. Keep re-doing the codes that bucky
    has taught you, and embed it into your brains :). 2. Make a list of all the
    tutorials and mark them 0 – 100% of your understanding. 3. Put all the
    codes that you have created in order in some form of notepad file, this way
    you may refer to it and make notes if necessary. 4. Everyday, make sure
    that you go back and see if you remember some of the earlier tutorials.
    (This may seem like common sense, but some people …)

    Reply
  9. PLEASE PLEASE PLEASE HELP!!! when I ran the program in eclipse a message
    came up saying: Java Virtual Machine Launcher A Java Exception has
    occurred. PLEASE PLEASE PLEASE HELP!!!

    Reply
  10. package kyle; import java.util.Scanner; class dfsg { public static void
    main (String arg[]) { Scanner kyle = new Scanner (System.in); double fnum,
    snum, awnser; System.out.println("Enter first number: "); fnum =
    kyle.nextDouble(); System.out.println("Enter second number: "); snum =
    kyle.nextDouble(); awnser = fnum + snum; System.out.println(awnser); } }

    Reply
  11. Resource leak 'calculator' is never closed. Copied it word for word except
    for replacing bucky with calculator and putting calculator.close() fixes
    the error but I still get an error running it.

    Reply
  12. Some tricks for eclipse users: If you wanna' use Sytem.out.println();
    simply type syso and then hold control and press space. It auto completes
    this. Also, instead of using 'double' you can use 'int' to declare
    variables. This way should be a lot faster.

    Reply

Post Comment