Java user Input-10 Programs

--help cse
4 min readJan 28, 2022

--

How to get input from user in Java and there different types of input.

Java Scanner Class

Java Scanner class allows the user to take input from the console. It belongs to java.util package. It is used to read the input of primitive types like int, double, long, short, float, and byte. It is the easiest way to read input in Java program.

Syntax

1. Scanner sc=new Scanner(System.in);

Following fig.no.1.chart more useful for taking input at that particular type. So don’t worry about chart right now , I will explain one by one.

FIg.no.1::Java inputs

PROGRAM :1:

Example of integer input from user

The following example allows user to read an integer form the System .in .So we use

CODE:

import java.util.*;

class Print_Integer

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter integer number :”);

String a= sc.nextLine(); //taking input from user same as scanf in c

System.out.println(“OUTPUT :”+a);

}

}

OUTPUT:

Enter integer number :5

OUTPUT :5

PROGRAM 2:

Example of String Input from user

Let’s see another example, in which we have taken string input.using sc.nextLine() function.

CODE:

import java.util.*;

class UserInputDemo1

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter a string: “);

String str= sc.nextLine(); //reads string

System.out.print(“You have entered: “+str);

}

}

OUTPUT:

Enter a string: somnath

You have entered: somnath

Program 3:Example of Taking float number

CODE:

import java.util.Scanner;

class floatexample

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter a float: “);

float flt= sc.nextFloat(); //reads string

System.out.print(“You have entered: “+flt);

}

}

OUTPUT:

Enter a float: 5.6

You have entered: 5.6

Program 4:Example of Taking Double number

This program is simlar to previous one only difference is that we have taken input as double and function are used nextDouble().

CODE:

import java.util.Scanner;

class doubleexample

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter a double: “);

double dbl= sc.nextDouble(); //reads string

System.out.print(“You have entered: “+dbl);

}

}

OUTPUT:

Enter a double: 1.99988

You have entered: 1.99988

Program 5:

CODE:

import java.util.Scanner;

class booleanexample

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter a Boolean: “);

boolean bl= sc.nextBoolean(); //reads string

System.out.print(“You have entered: “+bl);

}

}

OUTPUT:

Enter a Boolean: false

You have entered: false

This is 5 program about taking input of a different type.

Now we will do some operation on that we have taken different type of input.

Program 6:Addition of two integer using nextint()

Expln:This program addition of 3 integer numbers usig nextint()

CODE:

import java.util.*;

class UserInputthreeadd

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter first number- “);

int a= sc.nextInt();

System.out.print(“Enter second number- “);

int b= sc.nextInt();

System.out.print(“Enter third number- “);

int c= sc.nextInt();

int d=a+b+c;

System.out.println(“Total= “ +d);

}

}

OUTPUT:

Enter first number- 5

Enter second number- 6

Enter third number- 8

Total= 19

Program 7:Multiplication of two integer using nextint()

Expln:This program of a multiplication of 2 integer numbers usig nextint()

This program is similar to previous one only we are taking two integers and mutilpying.

CODE:

import java.util.*;

class MultiplicationUserInputDemo

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter first number- “);

int a= sc.nextInt();

System.out.print(“Enter second number- “);

int b= sc.nextInt();

int d=a+b;

System.out.println(“Multiplication of two numbers = “ +d);

}

}

OUTPUT:

Enter first number- 5

Enter second number- 6

Multiplication of two numbers = 30

*This way you can perform Multiplication,Subtraction and division and other.

Program 8:merging of two string using + and taking input through nextline()

CODE:

import java.util.*;

class Mergingstring

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter a string a: “);

String a= sc.nextLine();

System.out.print(“Enter a string b: “);

String b= sc.nextLine(); //reads string

String c=a+” “+b;

System.out.print(“You’r merging result of string: “+c);

}

}

OUTPUT:

Enter a string a: Harshad

Enter a string b: Mehta

You’r merging result of string: Harshad Mehta

Program 9:Printing table using for loop and input taking through nextint().

CODE:

import java.util.*;

class printuptofive

{

public static void main(String[] args)

{

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print(“Enter number to yo want print table:”);

int n= sc.nextInt(); //reads string

for(int i=1;i<10;i++) {

System.out.print(n*i); //multiplay one by one

System.out.print(“\n”);//for newline

}

}

}

OUTPUT:

Enter number to yo want print table:10

10

20

30

40

50

60

70

80

90

100

Program 10:Taking array as input and printing array using for loop.

CODE:

import java.util.Scanner;

public class ArrayInputExample1

{

public static void main(String[] args)

{

int n;

Scanner sc=new Scanner(System.in);

System.out.print(“Enter the number of elements you want to store: “);

//reading the number of elements from the that we want to enter

n=sc.nextInt();

//creates an array in the memory of length 10

int[] array = new int[10];

System.out.println(“Enter the elements of the array: “);

for(int i=0; i<n; i++)

{

//reading array elements from the user

array[i]=sc.nextInt();

}

System.out.println(“Array elements are: “);

// accessing array elements using the for loop

for (int i=0; i<n; i++)

{

System.out.println(array[i]);

}

}

}

OUTPUT:

Enter the number of elements you want to store: 5

Enter the elements of the array:

1

2

3

4

5

Array elements are:

1

2

3

4

5

Thank you.

--

--

--help cse
--help cse

Written by --help cse

I'm a Full-Stack Developer | AWS Certified DevOps Engineer, with passionate about coding and enjoy sharing my knowledge with others.

No responses yet