Take Input from user in Java

 import java.io.*; // first line step 1

public class DemoInput2 {

public void abc(String name,int age)

{

System.out.println("name "+name+" age "+age);

}

public void calc() throws IOException , NumberFormatException

{

BufferedReader brr = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter no1");

//String n = br.readLine();  // input 1

//String na= n.trim();

//int nn = Integer.parseInt(na);

// short way

int nn = Integer.parseInt(brr.readLine().trim());

System.out.println("enter no2");

//String n1 = br.readLine(); // input 2

//int nn1 = Integer.parseInt(n1);

int nn1 = Integer.parseInt(brr.readLine().trim());

int sum = nn+nn1;

System.out.println(sum);

}


public static void main(String[] args) throws IOException  // step 2

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter name");

String n = br.readLine();  // input 1

System.out.println("enter age");

String a = br.readLine(); // input 2

// int x = a; // implicit

//int x = (int)a; // explicit

// convert String to short

short s=Short.parseShort(a);

// convert String to byte

byte b= Byte.parseByte(a);

// convert to int

int i=Integer.parseInt(a);

DemoInput2 d = new DemoInput2(); // default constructor

d.abc(n, i);

d.calc();

}


}


Comments

Popular posts from this blog

Prime Number

star pattern

Basic Calc using Python Tkinter