This simple example reads an input text file, in.txt, prints the file to standard output one character at a time and prints the total number of '(' characters and the total number of ')' characters.
1
2 public static main(String[] args) {
3 Scanner in = MyIO.openInput("in.txt");
4 in.useDelimiter("");
5 int lparenCount = 0;
6 int rparenCount = 0;
7
8 while(in.hasNext()) {
9 String s = in.next();
10 char ch = s.charAt(0);
11 System.out.print(ch);
12 if (ch == '(' ) {
13 lparenCount++;
14 } else if (ch == ')' ) {
15 rparenCount++;
16 }
17 }
18 System.out.printf("\nThere were %d '(' and %d ')'\n",
19 lparenCount, rparenCount);
20 }