Hi All,
Here is an example of how to get an int from
System.in; enjoy!
Regards,
- DL
import java.io.*;
import java.util.*;
public class MyReader {
static BufferedReader br =
new BufferedReader(
new InputStreamReader(
System.in));
public static String getString(
String prompt){
String s = null;
System.out.print(prompt);
try {
s = br.readLine();
}
catch(IOException e) {}
return s;
}
public static int getInt(String prompt) {
String s = getString(prompt);
int i=0;
try {
i = Integer.parseInt(s);
}
catch (NumberFormatException e) {
System.out.println(
s+" is not a valid int, try again");
return getInt(prompt);
}
return i;
}
public static int random(int ub) {
return (int)(Math.random()*ub + 1);
}
public static void main(String args[]) {
int i= getInt("please enter an int:");
System.out.println("you typed "+i);
System.out.println(random(50));
Random r = new Random();
int j = 0;
for (int k=0; k < i ; k++) {
j = r.nextInt();
System.out.println(j);
}
}
}
|