|
1 | 1 | package org.teachingkidsprogramming.recipes.inDevelopment; |
2 | 2 |
|
| 3 | +import java.util.Iterator; |
3 | 4 | import java.util.Scanner; |
4 | 5 |
|
5 | 6 | public class FunWithScanner |
6 | 7 | { |
7 | 8 | private static Scanner scanner; |
8 | | - private static Scanner scanner2; |
9 | 9 | public static void main(String[] args) |
10 | 10 | { |
11 | | - scanner = new Scanner(System.in); |
12 | | - scanner.useDelimiter("\\n"); |
13 | | - System.out.print("Enter an index: "); |
14 | | - int index = scanner.nextInt(); |
15 | | - System.out.print("Enter a sentence: "); |
16 | | - String sentence = scanner.next(); |
17 | | - System.out.println("\nYour sentence: " + sentence); |
18 | | - System.out.println("Your index: " + index); |
| 11 | + makeAString(); |
19 | 12 | } |
20 | 13 | public static void makeAString() |
21 | 14 | { |
22 | | - String input = "1 fish 2 fish red fish blue fish"; |
23 | | - scanner2 = new Scanner(input); |
24 | | - Scanner s = scanner2.useDelimiter("\\s*fish\\s*"); |
25 | | - System.out.println(s.nextInt()); |
26 | | - System.out.println(s.nextInt()); |
27 | | - System.out.println(s.next()); |
28 | | - System.out.println(s.next()); |
| 15 | + String input = "1 fish 2 fish red fish blue fish, "; |
| 16 | + scanner = new Scanner(input); |
| 17 | + System.err.println("\nIt is said that there are: " + input); |
| 18 | + Scanner s = scanner.useDelimiter("\\s*fish\\s*"); |
| 19 | + System.out.println("So: " + s.nextInt() + " and " + s.nextInt()); |
| 20 | + System.out.println("And: " + s.next() + " and " + s.next() + '\n'); |
| 21 | + for (String unit : new Iterable<String>() |
| 22 | + { |
| 23 | + private String units = "black fish,blue fish,old fish,new fish"; |
| 24 | + @Override |
| 25 | + public Iterator<String> iterator() |
| 26 | + { |
| 27 | + Scanner scanner = new Scanner(units); |
| 28 | + scanner.useDelimiter(","); |
| 29 | + return scanner; |
| 30 | + } |
| 31 | + }) |
| 32 | + { |
| 33 | + System.out.println("And also: " + unit); |
| 34 | + } |
29 | 35 | s.close(); |
30 | 36 | } |
31 | 37 | } |
0 commit comments