You are interested in analyzing the frequency of numbers selected for the Powerb
ID: 3683025 • Letter: Y
Question
You are interested in analyzing the frequency of numbers selected for the Powerball lottery. You have downloaded the previous year’s Powerball numbers which are saved in a text file called pb_2014.txt and each record in the file has the following format: 01/01/14 15 24 40 48 52 23 x PP 01/04/14 19 20 37 41 58 14 x PP 01/08/14 10 28 39 47 58 22 x PP 01/11/14 10 15 33 48 54 34 x PP Unfortunately this is an incompatible file format for the program you are using to analyze the frequency. The correct record file format needed by the program is shown below: 15 24 40 48 52 23 19 20 37 41 58 14 10 28 39 47 58 22 10 15 33 48 54 34 Write a program to convert the pb_2014.txt file into the required file format shown above saving the new format to the file powerball.txt.
Explanation / Answer
import java.io.*; class Test{ public static void main(String[] args) { StringBuilder builder = new StringBuilder(); BufferedReader br = null; try { String line; br = new BufferedReader(new FileReader("pb_2014.txt")); while ((line = br.readLine()) != null) { for(String s : line.split(" ")){ if(s.contains("/")||s.contains("x")||s.contains("PP")) continue; builder.append(s); } builder.append(" "); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } try { String content = builder.toString(); File file = new File("pb_2014.txt"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.