Java Scanner Input

OK guys now we are going to learn about the first step to interact with computer by giving an input to a running program.

Imagine you have created a simple calculator program and you need to add two numbers 134.43+238.92.

Now it would be very nice if that program is written in such a way that when it is running, it asks you to input any two numbers so the program will add them. It will be like this…

Enter your 1st number: 134.43   

Enter your 2nd number: 238.92

Sum of the two numbers you entered is: 373.35

The numbers 134.43 and 238.92 marked in blue are entered by the user while the program is running.

Too bad guyz, java is very unfriendly in this case. Can you remember the output is very easy to write?

It is just


But the input is a little bit “not understandable” yet:)

For now you may just type the code in the same way that I’ve written and later, in more advanced lessons series like Object Oriented Programming and Input Output Streams, you will get the full understanding about them.

No worries now!!

Now for our previous example, the whole code is like this. You may copy this and paste it in a new Class file and run this.

Now the name of your newly creating class should match with the name SimpleAdditionCalculator.

Let’s see the new features. I’ll explain it a little bit.

java.util.Scanner is the location of a Class file called Scanner.  The import statement should be located just above the Class name declaration statement.

This Scanner is another java type.

So userInput is nothing but a variable of type Scanner. Got it??

The term

is quite difficult to explain now but just think that it is the way to create a new Scanner item and that item is assigned to the userInput just like String myString = “OMG”;

num1 and num2 are just two Double variables used to store the two user inputs. Now in the next statement, see, I didn’t use the println. It’s just print.

Try modifying the code to println and you’ll see that the input should be entered in the next line, because moving to a new line is requested by the “ln” part in println.

Just like Strings, this Scanner variable userInput can call various methods. When the nextDouble() method is called, the program will wait until you enter some Double value to the Console.

Once you enter the two double values, the program will add two numbers and give the output. The final code part should now be obvious and you should be able to code that with eyes closed 🙂

Other than nextDouble() method, there are various other methods such as

  • nextInt() – takes the integer input from the user
  • next() – takes the String input from the user
  • nextBoolean() – takes the boolean input from the user

One final note before the end. What happens if you violate the agreement and enter a word instead of a number??

I bet you’ve tried it. Didn’t you? 🙂 If not, try it and see what happens.

An error message will come and program crashes.

So don’t cheat yet. Of course these errors can be handled. But not right now. So you have to be loyal and enter the correct type.

OK, now here’s a small exercise for you. Can you make a program that asks your name and prints a greeting message for you?? The Console should be something like this…

Please enter your name: Alex

Hello Alex, welcome to java world!

You can find the code below. But better to try yourself first. 🙂