1 /************************************************
  2 * This class, Lab5a.java ...
  3 *
  4 * @author:  Elizabeth Adams
  5 * Date:     01/26/05
  6 *
  7 */
  8
  9 //************************************************
 10 // Honor Statement:  This work conforms to the JMU
 11 //    Honor Code and the academic rules for this class.
 12 // Acknowledgement:  Indicate any student help or other
 13 //    references for this program not directly given in the
 14 //    assignment.  Include TA name if applicable.
 15 //************************************************
 16
 17
دددimport java.util.Scanner;
 18
دددimport java.io.*;
 19
 20
دصض×public class Lab5a
 21
دد§{
 22
دد§/** main method - what do I do?
 23
دد§*/
 24
دد§دقكàpublic static void main (String args []) throws IOException
 25
دد§دد§{
 26
دد§دد§// These are required variables.  Add any other variables needed
 27
دد§دد§// below these required ones. 
 28
دد§دد¨¹يدScanner myKeyboardScanner, myFileScanner, lineScanner;
 29
دد§دد¨¹يدboolean fileFound, numberFound;
 30
دد§دد¨¹يدString inputFileName, goodOutputFileName,badOutputFileName;    
 31
دد§دد¨¹يدString dataLine;
 32
دد§دد¨¹يدString badValue;
 33
دد§دد¨¹يدint goodValue;
 34
دد§دد¨¹يدFileWriter myGoodFileWriter;
 35
دد§دد¨¹يدBufferedWriter myGoodBufferedWriter;
 36
دد§دد¨¹يدPrintWriter myGoodPrintWriter;
 37
دد§دد¨¹يدFileWriter myBadFileWriter;
 38
دد§دد¨¹يدBufferedWriter myBadBufferedWriter;
 39
دد§دد¨¹يدPrintWriter myBadPrintWriter;
 40
دد§دد¨¹يدint smallest ;
 41
دد§دد¨¹يدint thirteenCount, valueCount ;
 42
دد§دد§  
 43
دد§دد§      // sets up myKeyboardScanner for keyboard input
 44
دد§دد¨¹¹دmyKeyboardScanner = new Scanner (System.in);
 45
دد§دد§
 46
دد§دد§      // prompt user for input file name
 47
دد§دد¨¹¹دSystem.out.println
 48
دد§دد§دددددد("Please enter the name of your input file with absolute pathname");
 49
دد§دد§
 50
دد§دد§     // get input filename from the user
 51
دد§دد¨¹¹دinputFileName = myKeyboardScanner.nextLine();
 52
دد§دد§
 53
دد§دد¨¹¹دfileFound = false;   // input file hasn't been found yet
 54
دد§دد§
 55
دد§دد¨¹¹®do
 56
دد§دد§دد5{
 57
دد§دد§دد7¹¹´try
 58
دد§دد§دد5دد§{ 
 59
دد§دد§دد5دد§   // to use the user's filename
 60
دد§دد§دد5دد¨¹¹دmyFileScanner = new Scanner (new File (inputFileName ) );  
 61
دد§دد§دد5دد¨¹¹دfileFound = true;  // was successful
 62
دد§دد§دد5دد§}
 63
دد§دد§دد5دد§دًîىcatch (FileNotFoundException fnfe) // failed
 64
دد§دد§دد5دد§دد§{
 65
دد§دد§دد5دد§دد¨¹¹دSystem.out.println
 66
دد§دد§دد5دد§دد§دددددد(" the file whose name you entered could not be found ");
 67
دد§دد§دد5دد§دد¨¹¹دmyFileScanner = new Scanner ("squib.txt");
 68
دد§دد§دد5دد§دد¨¹¹دSystem.out.println  // re-prompt user for input file name
 69
دد§دد§دد5دد§دد§دددددد("Please enter the name of your input file with absolute pathname");
 70
دد§دد§دد5دد§دد¨¹¹دinputFileName = myKeyboardScanner.nextLine(); // gets input filename from the user
 71
دد§دد§دد5دد©دد©}
 72
دد§دد§دد5} 
 73
دد§دد§ددٍwhile (!fileFound);  // try again if unsuccessful
 74
دد§دد§
 75
دد§دد§      // prompt user for output file name for good data
 76
دد§دد¨¹¹دSystem.out.println
 77
دد§دد§دددددد("Please enter the name of your output file for good data with complete path name");
 78
دد§دد§
 79
دد§دد§     // get output filename from the user for good data
 80
دد§دد¨¹¹دgoodOutputFileName = myKeyboardScanner.nextLine();
 81
دد§دد§
 82
دد§دد§       //prepare output file for writing good data
 83
دد§دد¨¹¹دmyGoodFileWriter     = new FileWriter(goodOutputFileName);     
 84
دد§دد¨¹¹دmyGoodBufferedWriter = new BufferedWriter (myGoodFileWriter);
 85
دد§دد¨¹¹دmyGoodPrintWriter    = new PrintWriter (myGoodBufferedWriter);
 86
دد§دد§
 87
دد§دد§      // prompt user for output file name for bad data
 88
دد§دد¨¹¹دSystem.out.println
 89
دد§دد§دددددد("Please enter the name of your output file for bad data with complete path name");
 90
دد§دد§
 91
دد§دد§     // get output filename from the user for bad data
 92
دد§دد¨¹¹دbadOutputFileName = myKeyboardScanner.nextLine();
 93
دد§دد§
 94
دد§دد§       //prepare output file for writing good data
 95
دد§دد¨¹¹دmyBadFileWriter     = new FileWriter(badOutputFileName);    
 96
دد§دد¨¹¹دmyBadBufferedWriter = new BufferedWriter (myBadFileWriter);
 97
دد§دد¨¹¹دmyBadPrintWriter    = new PrintWriter (myBadBufferedWriter);
 98
دد§دد§
 99
دد§دد¨¹¹±while ( myFileScanner.hasNext()) // while there is more data in the input file
100
دد§دد§دد5{
101
دد§دد§دد7¹¹دdataLine = myFileScanner.nextLine();  // reads the line
102
دد§دد§دد7¹¹دSystem.out.println (" picked up " +  dataLine );
103
دد§دد§دد7¹¹دlineScanner = new Scanner (dataLine); // creates the line Scanner
104
دد§دد§دد5  //lineScanner.useDelimiter(" ";
105
دد§دد§دد7¹¹±while (lineScanner.hasNext()) // there is more data on the line
106
دد§دد§دد5دد5{
107
دد§دد§دد5دد7¹¹´try // to get a number
108
دد§دد§دد5دد5دد§{
109
دد§دد§دد5دد5دد¨¹¹دgoodValue = lineScanner.nextInt(); // gets a number
110
دد§دد§دد5دد5دد¨¹¹دSystem.out.println (goodValue + " integer ");
111
دد§دد§دد5دد5دد¨¹¹دmyGoodPrintWriter.println (goodValue);
112
دد§دد§دد5دد5دد§}
113
دد§دد§دد5دد5دد§دًîىcatch (java.util.InputMismatchException ime)
114
دد§دد§دد5دد5دد§دد§{
115
دد§دد§دد5دد5دد§دد¨¹¹دbadValue = lineScanner.next();  // gets a String from line
116
دد§دد§دد5دد5دد§دد¨¹¹دSystem.out.println (badValue + " String");
117
دد§دد§دد5دد5دد§دد¨¹¹دmyBadPrintWriter.println (badValue);
118
دد§دد§دد5دد5دد©دد©}
119
دد§دد§دد5دد°} // end inner while
120
دد§دد§دد°} // end outer while
121
دد§دد¨¹¹دmyGoodPrintWriter.close();
122
دد§دد¨¹¹دmyBadPrintWriter.close();
123
دد§دد§  
124
دد§دد¨¹¹دSystem.out.println (" STARTING PART 2 ");
125
دد§دد§         // initialize variables for second part
126
دد§دد§        
127
دد§دد§        
128
دد§دد¨¹¹دsmallest = Integer.MIN_VALUE;
129
دد§دد¨¹¹دthirteenCount = 0;
130
دد§دد¨¹¹دfileFound = false;
131
دد§دد¨¹¹دnumberFound = false;
132
دد§دد¨¹¹دvalueCount = 0;  
133
دد§دد§                                
134
دد§دد§      // prompt user for input file name
135
دد§دد¨¹¹دSystem.out.println
136
دد§دد§دددددد("Please enter the name of your input file with absolute pathname");
137
دد§دد§
138
دد§دد§     // get input filename from the user
139
دد§دد¨¹¹دinputFileName = myKeyboardScanner.nextLine();
140
دد§دد§  
141
دد§دد¨¹¹®do
142
دد§دد§دد5{
143
دد§دد§دد7¹¹´try
144
دد§دد§دد5دد§{
145
دد§دد§دد5دد¨¹¹دmyFileScanner = new Scanner (new File (inputFileName));
146
دد§دد§دد5دد¨¹¹دfileFound = true;
147
دد§دد§دد5دد§}
148
دد§دد§دد5دد§دًîىcatch (FileNotFoundException fne)
149
دد§دد§دد5دد§دد§{
150
دد§دد§دد5دد§دد¨¹¹دSystem.out.println (" your file wasn't found ");
151
دد§دد§دد5دد§دد¨¹¹دSystem.out.println (" please enter your input filename again ");
152
دد§دد§دد5دد§دد¨¹¹دmyFileScanner = new Scanner ("junk.txt");
153
دد§دد§دد5دد§دد¨¹¹دinputFileName = myKeyboardScanner.nextLine();
154
دد§دد§دد5دد©دد©}
155
دد§دد§دد5}     
156
دد§دد§ددٍwhile (!fileFound); 
157
دد§دد§       
158
دد§دد§          
159
دد§دد¨¹¹±while ( myFileScanner.hasNext())  // process data
160
دد§دد§دد5{
161
دد§دد§دد7¹¹´try // to get a number
162
دد§دد§دد5دد§{
163
دد§دد§دد5دد¨¹¹دgoodValue = myFileScanner.nextInt();
164
دد§دد§دد5دد§   if (!numberFound)
165
دد§دد§دد5دد§د6§{
166
دد§دد§دد5دد§د6¨¹¹دsmallest= goodValue;
167
دد§دد§دد5دد§د6¾¹¹دnumberFound = true;
168
دد§دد§دد5دد§د6د}
169
دد§دد§دد5دد§د÷´else if (goodValue < smallest)  // is it smallest
170
دد§دد§دد5دد§د¶¾¹¹دsmallest = goodValue;
171
دد§دد§دد5دد§
172
دد§دد§دد5دد¨¹³´if (goodValue == 13)      // is it 13?
173
دد§دد§دد5دد§د¶¾¹¹دthirteenCount++;
174
دد§دد§دد5دد¨¹¹دvalueCount++;
175
دد§دد§دد5دد§
176
دد§دد§دد5دد§   // System.out.println (" integer " + temp + " found ");  // for debugging only
177
دد§دد§دد5دد§}      
178
دد§دد§دد5دد§/*  
179
دد§دد§دد5دد§catch (NumberFormatException nfe)  // this one won't occur
180
دد§دد§دد5دد§{
181
دد§دد§دد5دد§   junk = myFileScanner.next();  // bypass non-integer
182
دد§دد§دد5دد§   System.out.println (" non-integer " + junk + " found ");
183
دد§دد§دد5دد§}
184
دد§دد§دد5دد§*/  
185
دد§دد§دد5دد§دًîىcatch (java.util.InputMismatchException ime)
186
دد§دد§دد5دد§دد§{
187
دد§دد§دد5دد§دد¨¹¹دbadValue = myFileScanner.next();  // bypass non-integer
188
دد§دد§دد5دد§دد¨¹¹دSystem.out.println (" non-integer " + badValue + " found ");
189
دد§دد§دد5دد§دد§   valueCount++;
190
دد§دد§دد5دد©دد©}
191
دد§دد§دد°} // end while
192
دد§دد§  
193
دد§دد¨¹³´if (!numberFound)
194
دد§دد§د6¾¹¹دSystem.out.println(" there were no integers in the file");
195
دد§دد§دِ´else
196
دد§دد§د¸§{
197
دد§دد§د¸¨¹¹دSystem.out.println
198
دد§دد§د¸§دددددد(" The smallest number in the file was: " + smallest);
199
دد§دد§د¸¨¹¹دSystem.out.println
200
دد§دد§د¸§دددددد(" The number of thirteens in the file was: " + thirteenCount);
201
دد§دد§د¸¾¹¹دSystem.out.println
202
دد§دد§دبددددددد   ( " The number of values in the file was: " + valueCount);} 
203
دد§دد§  
204
دد§دد©} // main 
205
دد©}// end class