Thursday, February 3, 2011

Data Input using String (Java)

In the following example, the data is taken as input using Strings.


import java.io.* ;

public class DataInput {

public static void main(String args[]) {

try {
String data ; //declare the variable as per your choice of datatype
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter your name: ");
data = br.readLine();
System.out.println("Hello " + data);
} catch (IOException e) {
e.printStackTrace() ;
}

}

}

The output for the above program is as follows:
Enter your name: abc
Hello abc

No comments:

Post a Comment