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