---
title: "Various ways to take inputs in Java"  
description: "The java platform provides various ways by which you can read an input. These are as follow:1)Using the Scanner class: We can take inputs from user us"  
author: "Devesh Kumar Singh"  
published: 2015-11-06  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10980/various-ways-to-take-inputs-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Various ways to take inputs in Java

The [java platform](https://www.mindstick.com/interview/22895/what-is-the-main-difference-between-java-platform-and-other-platforms) provides various ways by which you can read an input. These are as follow:

1) Using the [Scanner class](https://www.mindstick.com/forum/161308/how-does-the-scanner-class-simplify-user-input-in-java): We can take inputs from [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) using the scanner class as follow:

```
import java.util.Scanner;class addscan{ public static void main(String arng[]){Scanner data = new Scanner(System.in);int num1, num2;System.out.println("Enter 1st number");num1 = data.nextInt();System.out.println("Enter 2nd number");num2 = data.nextInt();sum(num1,num2);}public static void sum(int numx, int numy){int sum=0;sum=numx+numy;System.out.println("Sum of 2 number:"+sum);}}
```

This will [produce](https://yourviews.mindstick.com/view/83488/how-much-power-do-solar-panels-produce) the following [output](https://www.mindstick.com/forum/161319/how-does-the-print-function-handle-output-in-python) in [command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)-Line:

\
![Various ways to take inputs in Java](https://www.mindstick.com/blogs/3d658cc2-3d39-4514-8816-cbdfb7cffebc/images/33a09797-b398-433a-a574-3d868b5d3bf1.png)

2) Using the BufferedReader and InputStreamReader classes: We can take inputs from user using these class as follow:

```
import java.io.*;class addtwo{public static void main(String args[]){BufferedReader br=new BufferedReader(new InputStreamReader(System.in));int x=0,y=0;try{System.out.println("enter first number");x=Integer.parseInt(br.readLine());System.out.println("enter second number");y=Integer.parseInt(br.readLine());}catch(IOException e){}System.out.println("Sum is:"+add(x,y));}public static int add(int x, int y){return (x+y);}}
```

This will produce the following output in command-Line:

![Various ways to take inputs in Java](https://www.mindstick.com/blogs/3d658cc2-3d39-4514-8816-cbdfb7cffebc/images/3711d9d0-5508-41b9-ae3b-d421b21a2f46.png)\

3) Using the DataInputStream classes: We can take inputs from user using these class as follow:

```
import java.io.*;public class datais{  public static void main(String args[]) throws IOException  {    DataInputStream dis = new DataInputStream(System.in);    System.out.println("Enter your name: ");    String str1 = dis.readLine();    System.out.println("I know your name is " + str1);    dis.close();  }}
```

This will produce the following output in command-Line:

\
![Various ways to take inputs in Java](https://www.mindstick.com/blogs/3d658cc2-3d39-4514-8816-cbdfb7cffebc/images/830f85a1-ad0c-4c9a-9498-d2ba08523f95.png)\

Since I have newer version of java, that’s why it is giving this note instead of the proper output. These APIs have been removed from the newer java because new classes have been added in place of it.

If you have an older version it will provide the following output:

Enter your name:

Devesh

I know your name is Devesh

\
4) Using the Console classes: We can take inputs from user using these class as follow:

```
import java.io.Console;class conso{public static void main(String args[]){Console console = System.console();String s = console.readLine("Name : ");System.out.println("Name entered:" +s);}} 
```

This will produce the following output in command-Line:

*\*![Various ways to take inputs in Java](https://www.mindstick.com/blogs/3d658cc2-3d39-4514-8816-cbdfb7cffebc/images/30a818b8-c4ce-47e3-9932-8f8a07843253.png)

---

Original Source: https://www.mindstick.com/blog/10980/various-ways-to-take-inputs-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
