Accept user data using Java IO Package;
import java.io.*;
public class accept {
public static void main(String[] args) throws IOException{
int a;
String b;
double c;
char d;
long e;
short f;
float g;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer: ");
a= Integer.parseInt(br.readLine());
System.out.println("Enter a string: ");
b= br.readLine();
System.out.println("Enter a double: ");
c= Double.parseDouble(br.readLine());
System.out.println("Enter a character: ");
d= (char) br.read();
System.out.println("Enter a long: ");
e= Long.parseLong(br.readLine());
System.out.println("Enter a short: ");
f= Short.parseShort(br.readLine());
System.out.println("Enter a float: ");
g= Float.parseFloat(br.readLine());
System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g);
}
}
To run the above code follow following steps:-
import java.io.*;
public class accept {
public static void main(String[] args) throws IOException{
int a;
String b;
double c;
char d;
long e;
short f;
float g;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter an integer: ");
a= Integer.parseInt(br.readLine());
System.out.println("Enter a string: ");
b= br.readLine();
System.out.println("Enter a double: ");
c= Double.parseDouble(br.readLine());
System.out.println("Enter a character: ");
d= (char) br.read();
System.out.println("Enter a long: ");
e= Long.parseLong(br.readLine());
System.out.println("Enter a short: ");
f= Short.parseShort(br.readLine());
System.out.println("Enter a float: ");
g= Float.parseFloat(br.readLine());
System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+e+"\n"+f+"\n"+g);
}
}
To run the above code follow following steps:-
- Save the above code with file name accept and extension .java
- Open terminal or cmd and move to location of file using cd command.
- To compile type: javac accept.java
- To run type: java accept
No comments:
Post a Comment