# Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

## "Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

How to take user input in java?:-

USER INPUT AND DISPLAY OUTPUT IN JAVA:-

We can take input in java using two different ways:-

1.1  using Command Line Input with Command Prompt or Terminal

1.2 using System.in where "in" is the reference variable of InputStream class

System.in will provide input handling operation using Three different ways

1.2.1 System.in.read():-  It raises an exception and we can take input only one char at a time, it returns ASCII code hence we never use this for User Input operation.

Program to take input using System.in.read()?

import java.io.*;
class UserInput1
{

public static void main(String args[]) throws IOException
{
int a;
System.out.println("enter char");
System.out.println((char)a);

}

}

1.2.2  System.in reference using Scanner class which exists under java.util package

Scanner sc = new Scanner(System.in);

Scanner is the input manager class which is used to contain data in Object Pattern, we can easily convert object type to primitive type using the predefined method.

int a = sc.nextInt();    //Convert Object to integer

float b = sc.nextFloat();    //Convert Object to float

double d = sc.nextDouble(); //Convert Object to Double

String s = sc.next()  //single word

String s1 = sc.nextLine()  //multiple word

char ch = sc.next().charAt(0);  //Object to String to char where 0 is a index position of string

1.2.3 System.in reference using the InputStreamReader class which is used to read multiple line input, it will be used in file handling operation.
we should import java.io package

Q)WAP to take input using System.in.read() and perform the addition of two numbers?

import java.io.*;
class InputDemo
{
public static void main(String args[]) throws IOException
{
System.out.println("enter first and second number");
int c=a+b;
System.out.println((char)c);

}

}

output c

import java.io.*;
class InputDemo
{
public static void main(String args[]) throws IOException
{
System.out.println("enter first and second number");
int a =System.in.read();  //49   //char to ascii
Object a1= (char)a;  //char to object
Object b1= (char)b;
int c= Integer.parseInt(a1.toString())+Integer.parseInt(b1.toString

());  //'1' '2'
System.out.println(c);

}

}

import java.util.Scanner;
class InputDemo
{
public static void main(String args[])
{
Scanner abc = new Scanner(System.in);
System.out.println("enter first and second number");
int a =abc.nextInt();
int b =abc.nextInt();
int c= a+b;
System.out.println(c);

}

}
{"ft_lang_label":"__label__en","ft_lang_prob":0.5083183,"math_prob":0.59487784,"size":3084,"snap":"2022-40-2023-06","text_gpt3_token_len":714,"char_repetition_ratio":0.14383116,"word_repetition_ratio":0.3632287,"special_character_ratio":0.25907913,"punctuation_ratio":0.19908814,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9792613,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-03T20:16:52Z\",\"WARC-Record-ID\":\"<urn:uuid:85e24bae-124f-41e4-8334-9651de2364f9>\",\"Content-Length\":\"301066\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8b66cf49-c72f-434b-a0fb-6d37fa7f6325>\",\"WARC-Concurrent-To\":\"<urn:uuid:a6a7b719-359f-4dc9-9be5-0e8e0242221a>\",\"WARC-IP-Address\":\"172.253.115.121\",\"WARC-Target-URI\":\"https://www.shivatutorials.com/2019/09/how-we-take-user-input-in-java.html\",\"WARC-Payload-Digest\":\"sha1:7EVOMYO4WG4PE5GAQFMS5MJOH327LOVN\",\"WARC-Block-Digest\":\"sha1:3PC4HKRVJKPDBILBWKAL5MCQLXXDVKTO\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500074.73_warc_CC-MAIN-20230203185547-20230203215547-00788.warc.gz\"}"}