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());
                }
            }
        }

this isn't working

Please explain\ what happens. If the code throws an exception, copy the full text and paste it here.

One problem I see is that you are not saving the String returned by the replace() method. The value of a String object can not be changed. The replace() method will return a new String that has had the changes done to it. The original String will not be changed.

Thanks, that was likely the cause. It was throwing a NumberFormatException when it called the parseDouble method. I'll test it tomorrow, and if it works I'll mark this solved and if it doesn't I'll post the full error message.

i got the solution and here is the modified code of your code :

public static void main(String[] args) 
    {
        try
        {
            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(new FileReader("c://survey.txt"));
            while (scan.hasNext()) {
                String temp = scan.nextLine();

                if (!temp.startsWith("#")) {
                    Scanner tokenizer = new Scanner(temp);

                    if (tokenizer.hasNext()) {                        
                        idNumbers.add(Integer.parseInt(tokenizer.next().trim()));   // changed here
                    }

                    if (tokenizer.hasNext()) {
                        String incomeTemp = tokenizer.next().trim();
                        if(incomeTemp.trim().contains("$")){
                            incomeTemp = incomeTemp.replace('$',' ');   // changed here
                        }
                        annualIncome.add(Double.parseDouble(incomeTemp));
                    }
                    if (tokenizer.hasNext()) {
                        numPeopleInHousehold.add(Integer.parseInt(tokenizer.next()));
                    }
                }
            }            
            System.out.println("idNumbers  :"+idNumbers);
            System.out.println("annualIncome :"+annualIncome);
            System.out.println("numPeopleInHousehold :"+numPeopleInHousehold);

        }catch(Exception e){
            e.printStackTrace();
        }
    }

input format is like as follows:

100 1.20 58
12 $258.0 6
102 325.0 566

check it once.let me know whether it is your actual requirement or not

i have done two minor changes of your code
1. instead of using nextInt() i used next() method ( this was given the problem in your code)
and
2. after replace the dollor sign of double number you have to reassign the resulttant value to the same object but you forgot it

because of these 2 reasons your code couldn't give the exact solution to your problem

thats all

yours,
radha krishna

note : let it be if any mistakes in my sentence formation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.