How to take user input in Java


How to take user input in java
JAVA
  • Syntax :
  1.       import java.util.Scanner
  2.       Scanner sc = new Scanner(System.in)

The syntax given above is use to take the input from the user.

The syntax import java.util.Scanner is a package & the  Scanner sc = new Scanner(System.in) is a class of the package.

you should have to declare both the syntax for taking user input.

The user input data is may be in the form of the integer, float, double, char, boolean etc.For taking input of diffrent type of data in java there is different syntax for different type of data which is given bellow.

  •  nextBoolean() - Reads a boolean value from the user.
  •  nextByte() - Reads a byte value from the user.
  •  nextDouble() - Reads a double value from the user.
  •  nextFloat() - Reads a Float value from the user.
  •  nextInt() - Reads a int value from the user.
  •  nextLine() - Reads a string value from the user.
  •  nextLong() - Reads a long value from the user.
  •  nextShort() - Reads a short value from the user.

Example of calculating simple interest for taking user input

PROGRAM –

package sssss;
import java.util.Scanner;
public class computertechnologer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int p = sc.nextInt();
float r = sc.nextFloat();
int n = sc.nextInt();
float si = p*r*n/100;
System.out.println("simple interest is : " + si);
}
}

OUTPUT –

 1000
1.5
2
simple interest is : 30.0

 In above example of calculating simple interest we declare variable ‘sc’ to the class Scanner and take the 2 integer for principle & period and 2 float value for rate & simple interest.

We all know the formula for simple interest which is p*r*n/100.

Then we just print the result.

5/5

The video lecture for taking user input


I hope you all like this article…!

Comment bellow if you have any problem regarding taking user input in java

Rate this post

Saurabh

A digital marketer, programmer, and WordPress developer who is always looking for new technology stuff and news. Saurabh has 1+ years of experience in writing content and in web development.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.