/* * Read in a text file with delimiters * Two class - OOP version * sorts into 3 different array sets one for each age group */ package textfileoopsort; public class TextFileSortUI { public static void main(String[] args) { TextFileSortManager sm = new TextFileSortManager(); sm.displayAll(); } } ==================================================================== /* * OOP version. * Reading in a text file with four separate values per line with sorting * p - planets for adult teams * m - moons for teenage teams * c - celetrial bodes for children teams * EXAMPLES: * Mercury#p#red and green#1767.99 * Astroid#c#orange#1784.87 * Io#m#light blue#2578.54 * * Display all method to display the contents of the 3 array sets * The total funds from adults, teenagers and children is determined */ package textfileoopsort; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JOptionPane; public class TextFileSortManager { // Separate array sets for the team name, one for the colours and one // for funds // adults private String [] teamNameA = new String[20]; private String [] teamColoursA = new String[20]; private double [] teamFundsA = new double[20]; private int sizeA = 0; // teenagers private String [] teamNameT = new String[20]; private String [] teamColoursT = new String[20]; private double [] teamFundsT = new double[20]; private int sizeT = 0; // children private String [] teamNameC = new String[20]; private String [] teamColoursC = new String[20]; private double [] teamFundsC = new double[20]; private int sizeC = 0; private double totalFunds = 0.0; TextFileSortManager(){ String name = null; String ageGroup = null; String colours = null; double funds = 0.0; // Populate the three array sets // Reads in a text file and separates the values, each into their own // array sets. A for adults, T for teenagers and C for children try { Scanner scFile = new Scanner(new File("SixASideBeachTextC.txt")); while (scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter("#"); // No sorting as we dont know the age group at this point name = scLine.next(); ageGroup = scLine.next(); colours = scLine.next(); funds = scLine.nextDouble(); // planets for adults if(ageGroup.equals("p")) { teamNameA[sizeA] = name; teamColoursA[sizeA] = colours; teamFundsA[sizeA] = funds; sizeA++; } // moons for teenagers if(ageGroup.equals("m")) { teamNameT[sizeT] = name; teamColoursT[sizeT] = colours; teamFundsT[sizeT] = funds; sizeT++; } // celestial bodes for children if(ageGroup.equals("c")) { teamNameC[sizeC] = name; teamColoursC[sizeC] = colours; teamFundsC[sizeC] = funds; sizeC++; } } } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Error! File not found"); System.exit(0); } System.out.println("Success"); } // end constructor public void displayAll() { System.out.println("================================================"); System.out.println("Adults teams"); System.out.println("================================================"); for (int i = 0; i < sizeA; i++ ) { System.out.println(teamNameA[i] + "\t" + "\t"+ teamColoursA[i] + "\t" + "\t" + teamFundsA[i]); totalFunds = totalFunds + teamFundsA[i]; } System.out.println("================================================"); System.out.println("Teenage teams"); System.out.println("================================================"); for (int i = 0; i < sizeT; i++ ) { System.out.println(teamNameT[i] + "\t" + "\t"+ teamColoursT[i] + "\t" + "\t" + teamFundsT[i]); totalFunds = totalFunds + teamFundsT[i]; } System.out.println("================================================"); System.out.println("Children teams"); System.out.println("================================================"); for (int i = 0; i < sizeC; i++ ) { System.out.println(teamNameC[i] + "\t" + "\t"+ teamColoursC[i] + "\t" + "\t" + teamFundsC[i]); totalFunds = totalFunds + teamFundsC[i]; } System.out.println(); System.out.println("============================================="); System.out.println("Total amount raised by all the teams is R" + totalFunds); } } // end manager class =================================================================================== TEXT FILE LOOKS LIKE THIS. p for planet, c for celestial body, m for moon Mercury#p#red and green#1767.99 Venus#p#red#1565.34 Mars#p#green#2316.39 Jupiter#p#white#1452.12 Saturn#p#white and black#2198.19 Uranus#p#green stripes#1554.98 Neptune#p#yellow and orange#1567.76 Astroid#c#orange#1784.87 Comet#c#red and yellow#2776.56 Rhea#m#blue#1655.45 Callisto#m#blue stripes#1589.34 Io#m#light blue#2578.54 Europa#m#purple and orange#1686.66 Ceres#c#blue and black#2686.44 Luna#m#purple#1654.34 Titan#m#black and red#1876.98 Hyperion#m#green and dark red#2876.34 Janus#m#grey and red#1879.45 Oberon#m#black and yellow#1879.53 Ariel#m#black stripes#2876.34 ================================================================================== OUTPUT LOOKS LIKE THIS. Ignore messy columns at this stage. run: Success ================================================ Adults teams ================================================ Mercury red and green 1767.99 Venus red 1565.34 Mars green 2316.39 Jupiter white 1452.12 Saturn white and black 2198.19 Uranus green stripes 1554.98 Neptune yellow and orange 1567.76 ================================================ Teenage teams ================================================ Rhea blue 1655.45 Callisto blue stripes 1589.34 Io light blue 2578.54 Europa purple and orange 1686.66 Luna purple 1654.34 Titan black and red 1876.98 Hyperion green and dark red 2876.34 Janus grey and red 1879.45 Oberon black and yellow 1879.53 Ariel black stripes 2876.34 ================================================ Children teams ================================================ Astroid orange 1784.87 Comet red and yellow 2776.56 Ceres blue and black 2686.44 ============================================= Total amount raised by all the teams is R40223.61 BUILD SUCCESSFUL (total time: 0 seconds)