I am writing some code to read some information from a text file. The file is essentially three columns of numbers. The first columns are ints, the second are doubles, and the third is ints. So I have created a tokenizer that goes through line by line and interprets the first token of the line as an int, the second as a double, and the third as an int. Problem is, sometimes the second column may contain a "$" sign at the start of it. So I have a check in place that if the number in the second column contains a "$" sign, I use the replace method to replace the "$" with nothing (""). However, this isn't working and I'm not sure why. I've set a breakpoint and have stepped through it, and it is correctly determining if there is a "$" or not, but then the replace method does nothing. Can anybody help me with where I have gone wrong?
public static void main(String[] args) {
ArrayList<Integer> idNumbers = new ArrayList<Integer>();
ArrayList<Double> annualIncome = new ArrayList<Double>();
ArrayList<Integer> numPeopleInHousehold = new ArrayList<Integer>();
System.out.println("Reading values:");
Scanner scan = new Scanner(
Survey.class.getResourceAsStream("/q1/survey.txt"));
while (scan.hasNext()) {
String temp = scan.nextLine();
if (!temp.startsWith("#")) {
Scanner tokenizer = new Scanner(temp);
if (tokenizer.hasNext()) {
idNumbers.add(tokenizer.nextInt());
}
if (tokenizer.hasNext()) {
String incomeTemp = tokenizer.next();
if (incomeTemp.contains("$")) {
incomeTemp.replace("$", "");
}
annualIncome.add(Double.parseDouble(incomeTemp));
}
if (tokenizer.hasNext()) {
numPeopleInHousehold.add(tokenizer.nextInt());
}
}
}