Jump to content
Team Avolition

Basic Java tutorial: Simple calculator

Recommended Posts

Hello users, I know this is nothing to Minecraft related, but I am learning Java and I would like to teach others what I am learning so they can become better at the Java language like me. Today I learned how to use Scanners and Standard output streams to make a simple calculator. So we will focus on the main details to create one.


What do you need?


* Eclipse - It is what I use and it is very simple.

* Basic Eclipse knowledge - How to open the program up and start a class in it.

* Really basic Java - Know what a semicolon is and what the brackets are used for etc.

* Eyes - I will go over almost every detail for better understanding so this tutorial is for beginners that are new to java.


What to keep in mind


This is my first tutorial on programming so I may not sound as clear.

I am a beginner on the Java language so if I can do it you can do it. I also appreciate opinions from the experts who are good at the Java language to tell me what I can work on.

This is really basic and I am pretty confident that I can help others struggling with this.




So let's open up Eclipse in whatever folder you have and create a class called, Calculator. At the top of "public class" we are going to import. Import all it does is it allows us to use certain things in our code like Scanners. So let's add 'import java.util.Scanner;' This will allow us to use Scanner. With this it will let us answer things to a question when the program tells us to. Remember also the semicolon is just a period in a sentence but in Java language!

Now go into the brackets( { } ) of the class and insert 'public static void main(String args[])'. It is basically java's name and it tells the difference between other mains. 'String arg[]' are just more strings in the code.



After that we are going to create the guts of the code that will make up the rest of the tutorial. Add some brackets under 'public static void main(String

args[])'. Here we will put in 'Scanner Calc = new Scanner(System.in);'. What we are doing is creating a new scanner called Calc which is short for Calculator in the system input.



Go down a line and we are going to add a Double. It changes a number to a string and changes a string to a number. So we got to name some strings we are going to use with Double. We are going to add three names which are going to be first for the first number choice and second for the second number choice and then answer for the sum up of the numbers. To do this do 'Double first, second, answer;' see how we did it in a single line instead of doing the code again and again? By adding a comma we can add more and more names but for now we are going to work with 3.



So what we did so far is added Double and are now going to write something. We do this with Standard output streams 'System.out.print("Example");'. We output print on the screen with this code and add parentheses and quotation marks in it to add text. Create one under the naming Double code and make it say that the user can enter their first number.



Now go down another line and we are going to let the user put the number they want. We want to call the Scanner so we can use it. So we put the code 'first = Calc.nextDouble();' With this we use the Double name (first) and our scanner name (Calc). the Double (First) is equal to the scanner (Calc) so whatever you put for the first number will be equal to (First) and then after that 'nextDouble()' is there so it moves to the next name we use which is (second) after we input our number.



To this point it is just repeating the code for the (second) Double. So go ahead and add another Standard output stream but this time saying that the user can enter the second number. Under that call the Double and the scanner again but with (second) as the name this time.



After this is math time! So go under the last code you written (which should be the calling Double and scanner code) and write some math. Whats good about Java is that it thinks for you, you just have to write the code down for it to think. For this tutorial we are going to have it be an addition calculator so what we do is just that, add the names up. For this one I am not giving code because it is self explanatory. Need help? Fine, think about this as you write, we want (answer) to equal the sum of (first) and (second). Remember to add a semicolon at the end!



So we got the math done now is time to add our final line of code. Write another Standard output stream and in it we want it to display the answer, but we don't know the answer unless we guess, so what we do is add the answer in the sentence! In the sentence first put " The total is." and after that we (add) the answer by simply putting it in there in it just after the quotations. It will look kind of like this ("example" + answer); .




Yes after this you just have to run the program and do as it says. This is how to make a simple calculator and this was just addition you can do subtraction, division, and multiplication etc. I first did this a while back and I learned the whys and hows of it and if I can do it so can you! If you got something wrong like an error or you are missing something then look back at this. If this tutorial was not clear to you then browse the internet for answers that is why it is there and well.. to do dumb stuff also. If you got it working but still don't know why this does that? Well just mess with it and see why you need it. Also mess around with it and see if you can add a third number in there or if you can add more text to make it more interesting. Play with it, play with it good and not in a sexy way. You will gain more knowledge like I am currently doing now and try out something more challenging to. Well I hope this was a good tutorial and feedback is always nice and tips are appreciated from the experts of Java. So yeah um bye :D.


~~Updated the text to make it more easier to navigate and Bolded important words.~~

- Czy

Edited by crazydude156
  • Upvote 1
  • Downvote 1

Share this post

Link to post
killernerd    18

i'm not really a fan of these kinds of "tutorials" tbh. Apart from the wall-of-text style of typing, it doesn't really explain anything.


IMO if you're trying to "teach the basics" you should either be explaining the syntax or an idea. Not both at the same time.

Share this post

Link to post
Matdo    148

This is really hard to read because you haven't used any formatting on it. Always use paragraphs and use the code tags. This will make it more structured and easier to read, rather than a wall of text.

Share this post

Link to post
rakiru    2713


>very simple



Anyway, remove the centre alignment - it makes it awkward to read.

  • Upvote 2

Share this post

Link to post
Brutus    14

Please be more precise with your language. You do not "need" Eclipse, nor any integrated development environment, to code a program in Java.

  • Downvote 3

Share this post

Link to post
rakiru    2713

Please be more precise with your language. You do not "need" Eclipse, nor any integrated development environment, to code a program in Java.

Please don't bump old threads unless you have something useful and relevant to say.


The list of things you need is for the tutorial, not to program in Java.

Share this post

Link to post
This topic is now closed to further replies.