Java: Scanner nextInt() hasNextInt() Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;

public class ScannerExample {

    public static void main(String[] args) {
        Vector vector = new Vector();
        Scanner scanner;
        try {
            scanner = new Scanner(new File("map.txt"));
            Integer i;
            while (scanner.hasNextInt()) {
                i = scanner.nextInt();
                vector.addElement(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("the elements of vector: " + vector);
        System.out.println(vector.size());
    }
}




Leave a Reply