//Author: ^-^ Veerle ^-^ import java.io.*; import java.util.Vector; public class currencyConverter2R //Class for reading the instruction txt file and creating the conversion matrix and currencies list { private float[][] conversionMatrix; //2D grid to store the conversion rates in private Vector vecCurrencies = new Vector(); //Vectors are arrays that can grow in size private BufferedReader readFile; private String strError = ""; //If there is an error, this message will be passed private boolean isError = false, noFile = false; //Error flags public currencyConverter2R () { } public Vector getCurrencies () { return vecCurrencies; } public float[][] getConversionMatrix () { return conversionMatrix; } public boolean isError () { return isError; } public String getErrorMsg () { return strError; } public boolean noFile () { return noFile; } private void reset () //Resets the flags and currency list { isError = false; noFile = false; vecCurrencies.clear(); } public void read () //Method that is called by the main programme { reset(); try //Tries the following code, if an error occurs it is catched { readFile = new BufferedReader(new FileReader("currencyConverter2Instructions.txt")); } catch (IOException errorMsg) //If an error occured, this code executes { isError = true; noFile = true; strError = "Error: Unable to find \"currencyConverter2Instructions.txt\""; } if (!isError) //If there was not an error with opening the file { readCurrencies(); } } private void readCurrencies () { String strBuffer = ""; //Stores the current line boolean isReadingCurrencies = false; //Flag for if currently reading currencies boolean hasReadCurrencies = false; //Flag for if currencies have all been read boolean isReadingRates = false; //Flag for if currently reading rates boolean hasReadRates = false; //Flag for if rates have all been read int column = 0, row = 0; //Row and column of the conversion matrix try { while ((strBuffer = readFile.readLine()) != null) //Keep looping until end of file { if (strBuffer.length() == 0) //If empty line { continue; } else if (strBuffer.charAt(0) == '/') //If a comment line { continue; } if (strBuffer.charAt(0) == '#') //Start of section { if (!hasReadCurrencies) //If haven't already read currencies { isReadingCurrencies = true; //Start reading currencies } else { isReadingRates = true; //Start reading rates } continue; } else if (strBuffer.charAt(0) == '~') //End of section { if (!hasReadCurrencies) { hasReadCurrencies = true; //Finish reading currencies isReadingCurrencies = false; int numberCurrencies = vecCurrencies.size(); conversionMatrix = new float[numberCurrencies][numberCurrencies]; //Create the conversion matrix with row/column = number currencies } else { hasReadRates = true; isReadingRates = false; } continue; } if (isReadingCurrencies) { vecCurrencies.addElement(strBuffer); } else if (isReadingRates) { int startIndex = strBuffer.indexOf('('); //Gets the char index number of the first time "(" is found int endIndex = strBuffer.indexOf(')'); String strRate = strBuffer.substring(startIndex + 1, endIndex); //Creates a new string containing the conversion rate that is bordered by ( and ) if (column >= vecCurrencies.size()) //Starts a new row when the column number is gt or eq to the number of currencies { column = 0; row++; } conversionMatrix[row][column] = Float.parseFloat(strRate); //Convert the current string conversion rate into a float, and put it into the conversion matrix column++; } } readFile.close(); //Closes the file } catch (IOException errorMsg) { isError = true; strError = "Error reading the file, check the synax of \"currencyConverter2Instructions.txt\""; } } }