[Home] [Syllabus] [Assignments and Labs] [Resources]
nextInt(), nextFloat(),
nextLine()
, etc. to parse most common types. It can be used to
read from a file or from a console. By default tokens (i.e. separate
chunks of input) are space-separated, but it is possible to set up a
scanner to use other delimiter characters.
import java.util.Scanner;
public class ReadingInts {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
int num_items = in.nextInt();
int [] items = new int[num_items];
for (int i = 0; i < num_items; ++i) {
items[i] = in.nextInt();
}
for(int i = 0; i < items.length; ++i) {
System.out.println(items[i]);
}
}
}