0% found this document useful (0 votes)
18 views

3.GP-Java-Taking Input

The document discusses two main ways to take input from the user in Java: using the Scanner class or using the BufferedReader class. The Scanner class is one of the most preferable ways as it can read primitive data types and strings. It provides methods like nextInt(), nextFloat(), nextLine() etc. Examples are given to take integer and string input from the user using the Scanner class.

Uploaded by

virat5.11.18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

3.GP-Java-Taking Input

The document discusses two main ways to take input from the user in Java: using the Scanner class or using the BufferedReader class. The Scanner class is one of the most preferable ways as it can read primitive data types and strings. It provides methods like nextInt(), nextFloat(), nextLine() etc. Examples are given to take integer and string input from the user using the Scanner class.

Uploaded by

virat5.11.18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Taking input in java

In Java, there are mainly two ways to get the input from the user.

● Using Scanner class


● Using BufferedReader class
1. Using Scanner class: Scanner is a class in java that is used to take input from the

user. It is present in the java.util package. Scanner class is one of the most preferable

ways to take input from the user. This class is used to read the input of primitive types

such as int, double, long, etc. and String. You need to import the java.util package

before using the Scanner class.

Methods of Scanner class in Java:

Java Scanner class provides various methods to read different primitive data types from
the user.

Method Description

nextInt() reads an int value from the user.

nextFloat() reads a float value from the user.

nextDouble() reads a double value from the user.

nextLong() reads a long value from the user.

nextShort() reads a short value from the user.

nextByte() reads a byte value from the user.

nextBoolean() reads a boolean value from the user.

nextLine() reads a line of text from the user.

next() reads a word from the user.

1
Example 1: Taking int value from the user

import java.util.Scanner;
class TakingInputFromUser {
public static void main(String argo[]) {

// Creating an object of Scanner class


Scanner sc = new Scanner(System.in);

// Read integer value from the user


System.out.println(“Enter first number :”);
int a = sc.nextInt();

System.out.println(“Enter second number :”);


int b = sc.nextInt();

// Adding two values


int c = a + b;

// Printing the sum


System.out.println(“Sum is : “ +c);
}
}

Output:

Enter first number : 10


Enter second number : 20
Sum is : 30

Example 2: Taking String from the user

import.java.util.Scanner;
class TakingInputFromUser {
public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a String : “);
// Read a string from the user
String str = sc.nextLine();

2
System.out.println(“Your entered string is : “ + str);
}
}

Output:

Enter a String : Coding Ninjas


Your entered string is : Coding Ninjas

You might also like