Simple input from the keyboardTag(s): IO
First method
import java.io.*;
public class TestReadLine {
public static void main (String args[]) {
StreamTokenizer Input=new StreamTokenizer(System.in);
try {
System.out.print(" Your first name : ");
Input.nextToken();
System.out.println("Hi " + Input.sval + "!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
java.io.DataInputStream in =
new java.io.DataInputStream(System.in);
String aLine = in.readLine();
Third method JDK1.1
In you program, use EasyInput.inputStr("<prompt>") to input a String or
EasyInput.InputInt("<prompt>") for an integer.
public class EasyInput {
public static int inputInt(String s) {
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
System.out.print(s);
int i =0;
try {
i = Integer.parseInt(input.readLine());
}
catch (Exception e) {
e.printStackTrace();
}
return i;
}
public static String inputStr(String s) {
String aLine = "";
BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
System.out.print(s);
try {
aLine = input.readLine();
}
catch (Exception e) {
e.printStackTrace();
}
return aLine;
}
public static void main(String s[]) {
while(true) {
int y = inputInt(" Year: ");
int m = inputInt("Month: ");
int d = inputInt(" Day: ");
String you = inputStr("Your name: ");
System.out.println(you + " " + y + m + d);
}
}
}
NOTE: JDK 1.5 provides the Scanner class to do this, see this HowTo.
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com