logo codesdope

Taking Input in Java


We make computer programs for users. We solve real-world computer problems for users. So, it is necessary for our computer to interact with the users. This is what you will learn in this chapter.

We are done with operators and know how to print something on the screen. In this section, we will learn about how to take input from a user. So, yeah now you can take input from a user and display something on the screen. Let's proceed.

In Java, we input with the help of the Scanner class. Java has a number of predefined classes which we can use. We will learn more about classes later.

Predefined classes are organized in the form of packages. This Scanner class is found in java.util package. So to use the Scanner class, we first need to include java.util package in our program.

We include a package in a program with the help of import keyword. We can either import the java.util.Scanner class or the entire java.util package.

To import a class or a package, add one of the following lines to the very beginning of your code.

import java.util.Scanner;   // This will import just the Scanner class
import java.util.*;   // This will import the entire java.util package

After importing, we need to write the following statement in our program.

Scanner s = new Scanner (System.in);

Here by writing Scanner s, we are declaring s as an object of Scanner class. System.in within the round brackets tells Java that this will be System Input i.e. input will be given to the system.

If you have understood all that has been stated till now, it's a good thing. If you haven't, do not bother. Currently, you just need to know that we have to import the class or package at the beginning of the program and then include the above line in the program.

How to Input


Taking a value from a user is quite easy. You just have to write a statement and it's done!

Let's start with integer. Consider the following code.

int n;
n = s.nextInt();      //   s is object of Scanner class

Statement n = s.nextInt(); is used to input the value of an integer variable 'n' from the user.

Here, nextInt() is a method of the object s of the Scanner class.

Let's see an example in which we will input an integer value from the user.

import java.util.*;
class I1{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter an integer");
    int a;
    a = s1.nextInt();
    System.out.println("The entered integer is" + n);
  }
}
Output
Enter an integer
14
The entered integer is14

a = s1.nextInt(); - s1.nextInt() will take integer input from the user and then whatever is entered, it will give us that. So, this statement will be like a = 14;, when user entered 14. It is like 's.nextInt()' gave us 14.

Similarly, we can input values of other data types also. Same as nextInt() is used to input an integer value, methods to input values of other data types are listed below.

Method Inputs
nextInt() Integer
nextFloat() Float
nextDouble() Double
nextLong() Long
nextShort() Short
next() Single word
nextLine() Line of Strings
nextBoolean() Boolean

Now let's see an example in which we will input values of different datatypes.

import java.util.*;
class I2{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter integer");
    int n = s1.nextInt();
    
    System.out.println("Enter double");
    double db = s1.nextDouble();
   
    System.out.println("Integer :" + n);
    System.out.println("Double :" + db);
  }
}
Output
Enter integer
45
Enter double
23.242
Integer :45
Double :23.242

Input Word


As we have seen in the above table, the method to input a word is next().

As we know that 10 is an integer, in the same way word is a string. A string is a collection of characters. Since word consists of characters, therefore it is a string.

We input a string value same as other datatypes, the only difference is the declaration of the string variable.

To input a word, we need to write

String n;
n = s.next();      //   s is object of Scanner class

or simply

String n = s.next();      //   s is object of Scanner class

Here, String is a class whose object is n. You need not go into the details of classes and objects right now as these topics are discussed in detail later in this tutorial.

Let's see an example for this.

import java.util.*;
class I3{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter your first name");
    String f = s1.next();
    System.out.println("Enter your last name");
    String l = s1.next();
    System.out.println("Your name is " + f + l);
  }
}
Output
Enter your first name
Robert
Enter your last name
Brown
Your name is RobertBrown
next takes only the first word if we try to give more than one word. Try it yourself.

Input a Sentence


In the same way as a word is a string, a sentence is also a string. Therefore, we input a sentence in the same way as that of word, the only difference is that the method to input it is nextLine().

String st = s.nextLine();      //   s is object of Scanner class

Consider an example.

import java.util.*;
class I4{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter your name");
    String name = s1.nextLine();
    System.out.println("Your name is " + name);
  }
}
Output
Enter your name
Robert Brown
Your name is Robert Brown
The only difference between the methods nextLine() and next() is that nextLine() reads the entire line including white spaces, whereas next() reads only upto the first white space and then stops.

An example will make the difference clear.

import java.util.*;
class I5{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter your name");
    String w = s1.next();
    System.out.println("Your name is " + w);
    System.out.println("Again enter your name");
    String st = s1.nextLine();
    System.out.println("Your name is " + st);
  }
}
Output
Enter your name
Robert Brown Jr.
Your name is Robert
Again enter your name
Your name is Brown Jr.

Here, next took only the first word. Rest of the words were taken by nextLine.

See this example

import java.util.*;
class I6{
  public static void main(String[] args){
    Scanner s1 = new Scanner(System.in);
    int l,b;
    System.out.println("Enter length of rectangle");
    l = s1.nextInt();
    
    System.out.println("Enter breath of rectangle");
    b = s1.nextInt();
    
    System.out.println("Area of rectangle is "+ (l*b));
  }
}
Output
Enter length of rectangle
2
Enter breath of rectangle
4
Area of rectangle is 8

You practice and you get better. It's very simple.
-Phillip Glass


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.