/Users/lyon/j4p/src/bookExamples/ch16Readers/QuestionParser.java

1    package bookExamples.ch16Readers; 
2     
3    import java.io.*; 
4     
5    import futils.*; 
6     
7    public class QuestionParser { 
8        public static void main(String args[]) { 
9            BufferedReader br = 
10                   futils.ReaderUtil.getBufferedReader( 
11                           "select a test file"); 
12           toConsole(br); 
13           futils.ReaderUtil.close(br); 
14       } 
15    
16       private static void promptUserTest() { 
17           BufferedReader br = new BufferedReader( 
18                   new InputStreamReader(System.in)); 
19           System.out.print(getUserAnswer( 
20                   "is java cool?", br)); 
21       } 
22    
23       public static String getString(BufferedReader br) { 
24           try { 
25               return br.readLine(); 
26           } catch (IOException e) { 
27               return null; 
28           } 
29       } 
30    
31       public static boolean getUserAnswer(String prompt, BufferedReader br) { 
32           System.out.print(prompt + ":"); 
33           String ans = getString(br); 
34           return ans.equals("t"); 
35       } 
36    
37       public static void readQuestion(String s) { 
38           System.out.println("question=" + s); 
39    
40       } 
41    
42       static public void toConsole(BufferedReader br) { 
43    
44           for (String line = ReaderUtil.readLine(br); 
45                line != null; 
46                line = ReaderUtil.readLine(br)) { 
47               System.out.println(line); 
48           } 
49           ReaderUtil.close(br); 
50       } 
51    
52   } 
53    
54   class Question { 
55       boolean answer = true; 
56       String question = null; 
57    
58       public static void test() { 
59           System.out.println( 
60                   new bookExamples.ch16Readers.Question("TRUE java is cool")); 
61           System.out.println( 
62                   new bookExamples.ch16Readers.Question("false fortran is cool")); 
63           System.out.println( 
64                   new bookExamples.ch16Readers.Question("wdlifjewoij java is cool")); 
65           System.out.println( 
66                   new bookExamples.ch16Readers.Question("true 4+4=8")); 
67       } 
68    
69       Question(String s) { 
70           int i = s.indexOf(' '); 
71           String ans = s.substring(0, i); 
72           question = s.substring(i); 
73           answer = (Boolean.valueOf(ans) 
74                   ).booleanValue(); 
75       } 
76    
77       public String toString() { 
78           return question + ":" + answer; 
79       } 
80   }