This is my current code:
ArrayList<String> arrayCurrency = new ArrayList<String>();
ArrayList<Integer> arrayRate = new ArrayList<Integer>();
ArrayList<String> arraySymbol = new ArrayList<String>();
public void loadFile() {
File file = new File("C://Users//me//Documents//TestingAssignment//Assignment 2 Part 2//src/currency2.txt");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
String line = in.readLine().trim();
while (line != null) {
String[] parts = line.split(",");
line = in.readLine();
String currency = parts[0].trim();
String rate = parts[1].trim();
String symbol = parts[2].trim();
int rateConverted = Integer.parseInt(rate);
//int thisRate = rateConverted2;
arrayCurrency.add(currency);
arrayRate.add(rateConverted);
arraySymbol.add(symbol);
}
for(String name:arrayCurrency) {
System.out.println(name);
}
for(int name:arrayRate) {
System.out.println(name);
}
for(String name:arraySymbol) {
System.out.println(name);
}
in.close();
} catch (Exception e) {
String msg = e.getMessage();
JOptionPane.showMessageDialog(null, msg);
}
}
I have gone through the code with a debugger. It happens when it attempts to convert the first number in the text file.
An example of how the .txt file is set out:
US Dollars, 1.60, $
I want to split this up into 3 different arrays. However, I am having a problem trying to convert parts[1].trim();
into an Integer so that it can be stored in ArrayList<Integer> arrayRate = new ArrayList<Integer>();
.
The error message shown is: "For Input String:" 1.60".
Any help is appreciated. Thanks.
1.6 is not an Integer. You will need to store it in a Double List and convert to double using methods of Double class.
ArrayList<Double> arrayRate = new ArrayList<>();
Double dnum = Double.parseDouble(str);
Use this link for reference - https://beginnersbook.com/2013/12/how-to-convert-string-to-double-in-java/