|
Java Input Setup
Choose from the following links, select all and Program > Run Selected to setup an Input library:
After running any of the above Java input libraries, you can run the corresponding test program (found below).
Or, use the "standard" Java input (not simple). import java.io.*, and don't forget to throw an Exception
/**
@Author: K. Weiser
Date: Aug 06, 2003
Import java.io.* for input
*/
import java.io.*;
public class Example {
public static int strToInt(String s) {
int result;
Integer i;
result = 0;
try {
i = new Integer(s);
result = i.intValue();
} catch (NumberFormatException e) {
System.out.println("BAD PARAMETER VALUE: ");
}
return result;
}// end strToInt
public int getInput() throws Exception{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
String s = keybd.readLine();
int result = strToInt (s);
return result;
}// end getInput
public static void main(String args[]) throws Exception{
int iNum;
Example obj = new Example();
System.out.print("Enter an integer: ");
iNum = obj.getInput();
System.out.println("The integer you entered is "+iNum);
} // end main
}// end Example
/* RUN
Enter an integer: 345
The integer you entered is 345
*/ |
You can use the JJS class provided by PSVM, the makers of
JJ Classic (online Java) and JJ2.
Sample code:
String str1;
int iNum;
double dNum;
boolean bool;
JJS.startTimer();
str1 = JJS.inputString("Input a String to JJS.inputString()");
iNum = JJS.inputInt("Input an int to JJS.inputInt()");
dNum = JJS.inputDouble("Input an int to JJS.inputDouble()");
bool = JJS.inputBoolean("Input an int to JJS.inputBoolean()");
System.out.print("\n"+ str1+" "+" "+iNum+" "+dNum+" "+bool);
System.out.println(" took "+JJS.millisSinceStartTimer()+" milliseconds to enter.");
|
You can use the IO class that is modeled after the input shown in Advanced Placement Computer Science Study Guide by Fran Trees and Cay Horstmann.
Sample code:
String str1, str2;
int iNum;
double dNum;
boolean bool;
System.out.print("Input a String to IO.readLine() ");
str1 = IO.readLine();
System.out.print("Input a String to IO.readWord() ");
str2 = IO.readWord();
System.out.print("Input an int to IO.readInt() ");
iNum = IO.readInt();
System.out.print("Input a double to IO.readDouble() ");
dNum = IO.readDouble();
System.out.print("Input a boolean to IO.readBoolean() ");
bool = IO.readBoolean();
System.out.println("\n"+ str1+" "+str2+" "+iNum+" "+dNum+" "+bool);
Sample Run:
Input a String to IO.readLine() dog
Input a String to IO.readWord() cat
Input an int to IO.readInt() 129
Input a double to IO.readDouble() -3.14159
Input a boolean to IO.readBoolean() tRUe
dog cat 129 -3.14159 true
|
You can use the SavitchIn class.
Sample code:
String str;
int iNum;
double dNum;
boolean bool;
System.out.print("Input a String to SavitchIn.readLine() ");
str = SavitchIn.readLine();
System.out.print("Input an int to SavitchIn.readLineInt() ");
iNum = SavitchIn.readLineInt();
System.out.print("Input a double to SavitchIn.readLineDouble() ");
dNum = SavitchIn.readLineDouble();
System.out.print("Input a boolean to SavitchIn.readLineBoolean() ");
bool = SavitchIn.readLineBoolean();
System.out.println("\n"+ str+" "+iNum+" "+dNum+" "+bool);
Sample Run:
Input a String to SavitchIn.readLine() dog
Input an int to SavitchIn.readLineInt() 129
Input a double to SavitchIn.readLineDouble() -3.14159
Input a boolean to SavitchIn.readLineBoolean() tRUe
dog 129 -3.14159 true |
You can use the KeyboardReader class.
Sample code:
KeyboardReader kr = new KeyboardReader();
String str;
int iNum;
double dNum;
str = kr.readLine("Input a String to readLine() ");
iNum = kr.readInt("Input an int to readInt() ");
dNum = kr.readDouble("Input a double to readDouble() ");
System.out.println("\n"+ str+" "+iNum+" "+dNum);
Sample Run:
Input a String to readLine() dog
Input an int to readInt() 129
Input a double to readDouble() -3.14159
dog 129 -3.14159
|
You can use the EasyReader class.
Sample code:
EasyReader er = new EasyReader();
String str;
int iNum;
double dNum;
System.out.print("Input a String to er.readLine() ");
str = er.readLine();
System.out.print("Input an int to er.readInt() ");
iNum = er.readInt();
System.out.print("Input a double to er.readDouble() ");
dNum = er.readDouble();
System.out.println("\n"+ str+" "+iNum+" "+dNum);
Sample Run:
Input a String to er.readLine() dog
Input an int to er.readInt() 129
Input a double to er.readDouble() -3.14159
dog 129 -3.14159
|
|