Method 1:
Code: Select all
java.io.DataInputStream in = new java.io.DataInputStream(System.in);
String aLine = in.readLine();
Code: Select all
System.out.println("Please enter your telephone number");
String strPhone = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
strPhone = br.readLine();
Method 2 with exception handling:
Code: Select all
System.out.println("Please enter your telephone number");
String strPhone = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
strPhone = br.readLine();
}
catch (IOException err) {
System.out.println("Error reading line");
}
Code: Select all
import java.io.* ;
class Example1 {
public static void main(String args[])
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("Welcome To Java Input");
try {
System.out.println("Please Enter In Your First Name: ");
String firstName = bufRead.readLine();
System.out.println("Please Enter In The Year You Were Born: ");
String bornYear = bufRead.readLine();
System.out.println("Please Enter In The Current Year: ");
String thisYear = bufRead.readLine();
int bYear = Integer.parseInt(bornYear);
int tYear = Integer.parseInt(thisYear);
int age = tYear – bYear ;
System.out.println("Hello " + firstName + " You are " + age + " years old");
}
catch (IOException err) {
System.out.println("Error reading line");
}
catch(NumberFormatException err) {
System.out.println("Error Converting Number");
}
}
}
Code: Select all
x = Double.parseDouble(temp); // string temp converted to double x
y = Float.parseFloat(temp); // string temp converted to float y
i = Long.parseLong(temp); // string temp converted to long i
j = Integer.parseInt(temp); // string temp converted to int j
k = Short.parseShort(temp); // string temp converted to short k
m = Byte.parseByte(temp); // string temp converted to byte m
a = Boolean.valueOf(temp).booleanValue(); // string temp converted to boolean a