Java Programming Tutorial – 7 – Building a Basic Calculator

Home/Programming Video Tutorials/Java Video Tutorials/Java Programming Tutorial – 7 – Building a Basic Calculator

Java Programming Tutorial – 7 – Building a Basic Calculator

FavoriteLoadingAdd to favorites

Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts

Java468x60
By | 2017-06-04T01:05:28+00:00 June 4th, 2017|Java Video Tutorials|50 Comments

50 Comments

  1. Abdullah khan June 4, 2017 at 1:07 am - Reply

    Hy I am using sublimetext3 and whenever i enter a number and press enter "enter second number" should pop up
    I get nothing …and it is not functioning as a calculator..i mean it is not adding up anything

    enter first number:
    23.1

    This is what i get…yyyeaaakkkkk

  2. Dharmang Gajjar June 4, 2017 at 1:07 am - Reply

    Tip u can also type syso ctrl+space and then enter 🙂

  3. Sky Chansey June 4, 2017 at 1:07 am - Reply

    Thank you so much!!! You are a thousands times more understandable than my JAVA AP teacher. I’ll pass because of these vids THX!

  4. Karimo June 4, 2017 at 1:08 am - Reply

    WOW! This is a great video tutorial. Absolutely fantastic!

  5. Tamar Murphy June 4, 2017 at 1:09 am - Reply

    i didnt get all the zeros afterwards im not sure why but this really helped thanks 🙂

  6. Jesper Forsström Malm June 4, 2017 at 1:15 am - Reply

    I love your vidoes man. Helping me pass my exams. You are more inspering then my teachers! Keep up the good work:D

  7. Jayaramu Maddi June 4, 2017 at 1:17 am - Reply

    super perfection

  8. Roberto Đopa June 4, 2017 at 1:18 am - Reply

    For me it doesnt work with decimal numbers…only whole numbers…can anone help?

  9. toajaybhartiya June 4, 2017 at 1:19 am - Reply

    what is the difference between public void and public static void

  10. RedCapedDragon June 4, 2017 at 1:20 am - Reply

    I get this error when I try and run the program: Exception in thread "main" java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at bucky.applesnotfail.main(applesnotfail.java:11)
    Here is the program i had:
    package bucky;

    import java.util.Scanner;

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

    }

    Please can someone help me in the last tutorial I had a problem running it as well and I think it is because of the import.java.util.Scanner; If I don’t put the package bucky; I get an error as well, same with when I move it to a different line. I don’t know what to do

  11. John Parker June 4, 2017 at 1:20 am - Reply

    Finally watching these all in 2017. Back then I was a school kid when these videos were uploaded

  12. Kryštof Paulík June 4, 2017 at 1:22 am - Reply

    This tutorial is almost 8 years old, but still the best one on youtube 🙂

  13. Pickquick June 4, 2017 at 1:23 am - Reply

    Somehow i unfucked your code without even understanding it https://gyazo.com/01390594fee0f6a6499ace6c775ba4fe

  14. Liam Welsh June 4, 2017 at 1:25 am - Reply

    I’m brand new at programming java and I managed to make a better calculator just after watching this. It’s just a couple extra System.Out statements but I think it’s pretty cool I found this out by my self.

    package bucky;
    import java.util.Scanner;

    public class UserInput {
    public static void main (String[] args) {
    Scanner bucky = new Scanner(System.in);
    double fnum, snum, answer;
    System.out.println("Enter first Number");
    fnum = bucky.nextDouble();
    System.out.println("Enter second Number");
    snum = bucky.nextDouble();
    System.out.print(fnum);
    System.out.print(" + ");
    System.out.print(snum);
    answer = fnum + snum;
    System.out.print(" = ");
    System.out.println(answer);

    bucky.close();

    }

    }

  15. Mohit Singh June 4, 2017 at 1:27 am - Reply

    Really helped me..

  16. Syed Minhaaj June 4, 2017 at 1:28 am - Reply

    Thanks a ton Bro! 🙂

  17. Roshan Jadhav June 4, 2017 at 1:28 am - Reply

    i am having problem with ECLIPSE .. Page for writing is not opening

  18. YouM4D June 4, 2017 at 1:28 am - Reply

    import java.util.Scanner;
    public class PracticeMethod {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("Enter your first Integer: ");
    int Fnumber = in.nextInt();

    System.out.print("Enter your second Integer: ");
    int Snumber = in.nextInt();

    int Result = Fnumber + Snumber;
    System.out.println(Fnumber +" + "+ Snumber +" = "+ Result);

    Result = Fnumber – Snumber;
    System.out.println(Fnumber +" – "+ Snumber +" = "+ Result);

    Result = Fnumber * Snumber;
    System.out.println(Fnumber +" * "+ Snumber +" = "+ Result);

    Result = Fnumber / Snumber;
    System.out.println(Fnumber +" / "+ Snumber +" = "+ Result);

    Result = Fnumber % Snumber;
    System.out.println(Fnumber +" % "+ Snumber +" = "+ Result);

  19. kevin brown June 4, 2017 at 1:29 am - Reply

    copied example to letter didn’t work error on line 5got in to work by adding in private static Scanner bucky; in line 4and check my clock settings and it had the . there and also I tried it with bucky.close(); and didn’t working but my question is why doesn’t this example work if the java language is all the same.

  20. Scott Ferdinands June 4, 2017 at 1:30 am - Reply

    So the .nextDouble() method is saying to print the next variable on the next line? Bit confused on the exactly what the method is defining.

  21. Ducky Bhai June 4, 2017 at 1:31 am - Reply

    you have no idea how famous you are in my class… God bless you .. litterly the best teacher avaliable online

  22. Hi everyone I'II watching you June 4, 2017 at 1:32 am - Reply

    i learn so much about java programming on my campus but I’m not make that basic calculator before. this video improve my skills

  23. Alex Janaqi June 4, 2017 at 1:32 am - Reply

    What about the decimal points explanation?

  24. Affe Elander June 4, 2017 at 1:34 am - Reply

    Getting a "Resource leak: ‘scannername’ is never closed
    Any fixes for this?
    Thanks in advance

  25. Ren N June 4, 2017 at 1:34 am - Reply

    This is so old, but seriously, thank you for taking the time to make this. My teacher gives us the information like the import and class, but doesn’t explain it, so I’ve been copy+pasting while not understanding how things work. This is so helpful <3

  26. Ebrima Jassey June 4, 2017 at 1:37 am - Reply

    I love how the views are steadily decreasing with each video lmao

  27. Dimitur Georgiev June 4, 2017 at 1:38 am - Reply

    what am I doing wrong?

    Enter first number:
    34.1
    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 novo.apples.main(apples.java:8)

  28. Po op June 4, 2017 at 1:39 am - Reply

    package PoopPackage1;
    //[Made by Nic];
    import java.util.Scanner;
    import java.util.*;
    public class Swqdfqewfqef {

    public static final double number = 23.4333111;

    private String name;
    private int weight;
    private boolean hasOwner = false;
    private byte age;
    private long uniqueID;
    private char favoriteChar;
    private double speed;
    private float height;

    protected static int numberOfAnimals = 0;

    static Scanner userInput = new Scanner(System.in);

    public Swqdfqewfqef() {
    numberOfAnimals++;

    int sumOfNumbers = 5 – 1;
    System.out.println("5 – 1 = " + sumOfNumbers);

    int diffOfNumbers = 5 + 1;
    System.out.println("5 + 1 = " + diffOfNumbers);

    int multOfNumbers = 5 * 1;
    System.out.println("5 * 1 = " + multOfNumbers);

    int divOfNumbers = 5 / 1;
    System.out.println("5 / 1 = " + divOfNumbers);

    int modOfNumbers = 5 % 3;
    System.out.println("5 % 3 = " + modOfNumbers);
    System.out.println("Enter the name: n");

    //hasNextInt, hasNextFloat, hasNextDouble
    //hasNextByte
    //nextInt, nextFloat, nextdouble
    if(userInput.hasNextLine()){
    this.setName(userInput.nextLine());
    }
    this.setFavoriteColor();
    this.setUniqueID();
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getWeight() {
    return weight;
    }
    public void setWeight(int weight) {
    this.weight = weight;
    }
    public boolean isHasOwner() {
    return hasOwner;
    }
    public void setHasOwner(boolean hasOwner) {
    this.hasOwner = hasOwner;
    }
    public byte getAge() {
    return age;
    }
    public void setAge(byte age) {
    this.age = age;
    }
    public long getUniqueID() {
    return uniqueID;
    }
    public void setUniqueID(long uniqueID) {
    this.uniqueID = uniqueID;
    System.out.println("Unique ID set tp: " + this.uniqueID);
    }
    public void setUniqueID() {
    long minNumber = 1;
    long maxNumber = 1000000;

    this.uniqueID = minNumber + (long) (Math.random() *((maxNumber – minNumber) + 1));
    String stringNumber = Long.toString(maxNumber);
    int numberString = Integer.parseInt(stringNumber);
    System.out.println("Unique ID set tp: " + this.uniqueID);
    }
    public char getFavoriteChar() {
    return favoriteChar;
    }
    public void setFavoriteChar(char favoriteChar) {
    this.favoriteChar = favoriteChar;
    }
    public void setFavoriteChar() {
    int randomNumber = (int) (Math.random() * 126) + 1;
    this.favoriteChar = (char) randomNumber;
    if(randomNumber == 32){
    System.out.println("Favorite character set to Space");

    }else if(randomNumber == 10){
    System.out.println("Favorite character set to Newline");
    }else {
    System.out.println("Favorite character set to " + this.FavoriteChar);
    }
    if((randomNumber > 97) && (randomNumber < 122)){
    System.out.println("Favorite character is a lowercase letter");

    }
    System.out.println("Favorite character is a lowercase");
    }
    public double getSpeed() {
    return speed;
    }
    public void setSpeed(double speed) {
    this.speed = speed;
    }
    public float getHeight() {
    return height;
    }
    public void setHeight(float height) {
    this.height = height;
    }
    public static void main(String[] args){
    Swqdfqewfqef theSwqdfqewfqef = new Swqdfqewfqef();
    }
    }

  29. Luc Mulder June 4, 2017 at 1:41 am - Reply

    thanks man! this tutorial is beginning to be useful to make things, obviously the first couple of video’s are kinda basic but now its beginning to be interesting!! love it

  30. NeonLight June 4, 2017 at 1:44 am - Reply

    Everytime when i enter a double it gives me this 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 me.bryan.javatest.Bryan.calc(Bryan.java:16)
    at me.bryan.javatest.Main.main(Main.java:8)
    "

    When i input an integer the calculator works fine.

    This is my code:

    "
    public void calc() {

    Scanner input = new Scanner(System.in);
    //
    double numOne;
    double numTwo;
    double answer;
    //
    System.out.print("Enter your first number: ");
    numOne = input.nextDouble();
    //
    System.out.print("Enter your second number: ");
    numTwo = input.nextDouble();
    //
    answer = numOne + numTwo;
    System.out.println("Your final answer is: " + answer);
    }
    }

    "

  31. Tahir Hayat June 4, 2017 at 1:44 am - Reply

    Friends If you want to make a simple calculator using java IDE Netbeans … The difference between this tutorial and my tutorial is … this is console based calculator … i’ve shared a calculator in GUI … My channel name is tips & tricks…. Thanks

  32. HUMZA shahzad June 4, 2017 at 1:45 am - Reply

    import java.io.IOException;
    import java.util.Scanner;

    public class Claculator {

    public static void main(String[] args) throws IOException{
    char choice;
    Scanner comu = new Scanner(System.in);

    double first,second,answer;

    System.out.println("Enter First Number " );
    first = comu.nextDouble();

    System.out.println("Enter Second Number");

    second= comu.nextDouble();

    System.out.println("please enter what operation you’d like");
    System.out.println("1. ADD ");
    System.out.println("2. SUBTRACT");
    System.out.println("3. MULTIPLY");
    System.out.println("4. DIVIDE");
    System.out.println("5. GET REMAINDER");
    choice = (char) System.in.read();
    switch (choice){

    case ‘1’:
    answer = first+second;
    System.out.println(answer);
    break;
    case’2′:
    answer = first-second;
    System.out.println(answer);
    break;
    case’3′:
    answer = first*second;
    System.out.println(answer);
    break;
    case ‘4’:
    answer = first/second;
    System.out.println(answer);
    case ‘5’:
    answer = first%second;
    System.out.println(answer);
    break;

    }

    }
    }

  33. Parwinder Singh June 4, 2017 at 1:46 am - Reply

    you are obsessed with the name bucky 🙂

  34. Quorban Young June 4, 2017 at 1:46 am - Reply

    what if you wanted to add a menu for that program how would you?

  35. OmarDdosinq_ PVP June 4, 2017 at 1:48 am - Reply

    Why are the views lower every video? People getting scared and leaving the want to code java?

  36. Leena June 4, 2017 at 1:49 am - Reply

    thanks you saved me

  37. Harshil Ganapathi June 4, 2017 at 1:50 am - Reply

    Yeah, lol, i learned to make a simple calcularot in java ! 🙂

  38. Innovation June 4, 2017 at 1:51 am - Reply

    I am several tutorials farther but are there some practices I can do to help me memorize all of this stuff?

  39. Ove Games June 4, 2017 at 1:53 am - Reply

    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 Test.Test.main(Test.java:8)

  40. Scott Horn June 4, 2017 at 1:53 am - Reply

    play the video at half speed…hilarious.

  41. Hunter Zolomon June 4, 2017 at 1:55 am - Reply

    Who would didlil this ? like wtf

  42. Kistlak Rajapaksha June 4, 2017 at 1:56 am - Reply

    Great & Super Bro !! 😀

  43. Noroz Khan June 4, 2017 at 1:56 am - Reply

    when i add the subtract,devide and multiply options so the all the answer cames at oce when add two variable it adds the subtract them ad devide them how to do them seprately

  44. arewa June 4, 2017 at 2:00 am - Reply

    YOU’RE A LIFE SAVER. THANK YOU. I’M ACTUALLY UNDERSTANDING THIS. MY PROFESSOR IS ASIAN & I CAN’T UNDERSTAND HARDLY ANYTHING SHE SAYS WHICH MAKES MY PROGRAMMING 1 CLASS SO DIFFICULT BECAUSE I’M A BEGINNER. GOD BLESS YOU. I HAVE BEEN DELIVERT !!!!!

  45. Umang Agarwal June 4, 2017 at 2:00 am - Reply

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

  46. EchoSSBM June 4, 2017 at 2:01 am - Reply

    wow this is so much easier in c++

  47. damir dejanovic June 4, 2017 at 2:04 am - Reply

    Error: Main method not found in class apples, please define the main method as:
    public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application

    iam getting this message and i copied the whole code correct :/
    somebody help? 🙂

  48. Ranit Pal June 4, 2017 at 2:04 am - Reply

    you are a great Teacher

  49. Copper June 4, 2017 at 2:04 am - Reply

    This seems like something you could use to make one of those early text-based RPGs.

  50. RichS June 4, 2017 at 2:05 am - Reply

    Which software do you use for coding? ….. Cuz i ve got BlueJ.

Leave A Comment

*