In this lesson, you will learn how to take input in Java at runtime.
You can take run-time input from console. Below is the code snippet for it.
import java.io.*; class TakeInput { public static void main(String p[]) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any no. to add..."); String str = in.readLine(); int value = Integer.parseInt(str); int sum = value+25; System.out.println("Number after addition = "+sum); } }
Here "in.readLine()" method is used to get the string at run-time as input where in is declared as an object of BufferedReader class.
Integer.parseInt(str) converts the string into an integer number if possible.