Scanner class Methods in Java

Java- Input and Output

 We have learnt about some Decision Control Statements. In this post, We will learn to get input an print output through a java program.

Output

Output means to display the result on screen or on any output device like printer,monitor etc. We already used output statement in our programs previously. To output data on screen we use Java's print statement.
Syntax:
             System.out.println("Output line"+ anyVariable);

println() is the method used for printing output in new line. We can also use print() for displaying output in single line. We can print value of variable by using String Concatenation Operator(+). The anyVariable is the name of variable.

Example:
In the example above, you can see the use of println() and print() methods of System class.Like, other languages we can use \n space character to print output in new line as well as System.out.println(); without any output text in it. 

Input

We can provide input using input devices like Scanner,Keyboard and mouse.It is important to make program that can take input from users. Because you cannot always give input within program.To make a program input-able. You can use following basic steps,
  1. A Scanner class of Java, which is used to scan or read the input given by user.
  2. The Scanner class is in-built.It can be imported by using import statement which imports java.util.Scanner class.The util is the package which contain several important classes of Java.
  3. Now, in the main() , create an object of the Scanner class.
  4. Then , use some methods of Scanner class to input data.
Now, lets understand  this with an example,

Example:

Explanation: In the example above, we made a program to print the greatest number using the if-else statement. The difference is that we are now taking the numbers from user. In the previous programs we used to give a fixed input which made programs static. The benefit of using a input statement is it allows us to use dynamic inputs. 
The steps are as follows:
  1. In the STEP 1, we imported the Scanner.If you don't import this class, program will be erroneous
  2. In the STEP 2, we created the Object of Scanner class using new Operator. Never forget to pass System.in in the parentheses or round brackets.
  3. After STEP 2,in line 9 of program,the print statement is for user acknowledgement.If you don't write a  print statement user will not get an indication of inputting a value.
  4.  In the STEP 3, the variable a is read by Scanner using its nextInt() method ,which is used for integer values.To read float,double,String and character  values , use nextFloat(),nextDouble(),nextLine() and next().charAt(0) respectively. We have used input.nextInt(), input is object of Scanner class and is invoking nextInt() method of it using dot(.) operator.
  5. STEP 4 is same as STEP 3 it is used to input the second number.
  6. In the Console Output, first print statement asks user for input.
  7. In the second line of output, the user gave an input which is colored as green.
  8. After the both inputs, the program executes its if -else statement and Print the greatest number using print statement.
I hope this input method is clear to you,if not you can ask me in comments.This is important because now on we'll take inputs from user in further programs.

Experiment: Use input methods for different datatype that are mentioned in Step 4 in the Input section.Try to use print() and println() methods to understand difference between them.

Keep Learning ! Happy Coding !😇

Comments