/* * Reading in a text file with three separate values per line * Example: Comet#red and yellow#2776.56 * Print out the three arrays and give the total amount of * funds raised. */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import javax.swing.JOptionPane; public class SixASideBeachTextB { public static void main(String[] args) { // One array for the team name, one for the colours and one for funds String [] teamName = new String[20]; String [] teamColours = new String[20]; double [] teamFunds = new double[20]; int size = 0; double totalFunds = 0.0; // Populate the three arrays // Reads in a text file and separates the values, each into their own // array try { Scanner scFile = new Scanner(new File("SixASideBeachTextB.txt")); while (scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter("#"); teamName[size] = scLine.next(); teamColours[size] = scLine.next(); teamFunds[size] = scLine.nextDouble(); size++; } } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Error! File not found"); System.exit(0); } System.out.println("Success"); for (int i = 0; i < size; i++ ) { System.out.println(teamName[i] + "\t" + teamColours[i] + "\t" + teamFunds[i]); totalFunds = totalFunds + teamFunds[i]; } System.out.println(); System.out.println("============================================="); System.out.println("Total amount of funds raised by the teams is R" + totalFunds); } // end main } OUTPUT - Do not worry about neat columns at this stage. 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 Astroid orange 1784.87 Comet red and yellow 2776.56 Rhea blue 1655.45 Callisto blue stripes 1589.34 Io light blue 2578.54 Europa purple and orange 1686.66 Ceres blue and black 2686.44 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 ============================================= Total amount of funds raised by the teams is R40223.61