Skip to content

Commit db9205f

Browse files
committed
added to new recipe using Java Scanner w/@samanthalangit
1 parent b294944 commit db9205f

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
package org.teachingkidsprogramming.recipes.inDevelopment;
22

3+
import java.util.Iterator;
34
import java.util.Scanner;
45

56
public class FunWithScanner
67
{
78
private static Scanner scanner;
8-
private static Scanner scanner2;
99
public static void main(String[] args)
1010
{
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();
1912
}
2013
public static void makeAString()
2114
{
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+
}
2935
s.close();
3036
}
3137
}

0 commit comments

Comments
 (0)