Andrei0427
Andrei0427

Reputation: 573

Using indexOf() twice fails to detect second character occurence

I have a string, something like 9.555.555,00 and want to remove all the symbols and just keep the numbers, this is in String format.

I am using indexof to find the special characters and then a loop in order to skip the particular character when the loop gets to it so it doesnt append that character to the final string.

However, when doing this, the code seems to pick up the first occurence of the period sign but then indexOf() returns -1 the second time, even though there is another . in the string.

int dotIndex, commaIndex;

        dotIndex = tempBalance.indexOf('.');
        commaIndex = tempBalance.indexOf(',');

        for(int j = 0; j < tempBalance.length(); ++j){

            //System.out.println("Iteration: " + j + " ~ i @ : " + i);

            if(j == dotIndex){
                System.out.println("Current dot Index: " + dotIndex + " J: " + j + " : " + tempBalance);
                dotIndex = tempBalance.indexOf(j+1, '.');
                System.out.println("New dotIndex: " + dotIndex);
                continue;
            } else if(j == commaIndex){
                break;
            } else {

                tempString.append(tempBalance.charAt(j));
                //System.out.print("Found normal Number: " + tempBalance.substring(j, (j+1)));
            }

Output for system.out.println:

Current dot Index: 1 J: 1 : 9.955.458,23
New dotIndex: -1

Upvotes: 1

Views: 740

Answers (5)

cahn
cahn

Reputation: 1369

tempBalance.indexOf(j+1, '.') should be tempBalance.indexOf('.', j+1). See Javadocs for indexOf(int, int).

Upvotes: 2

boxed__l
boxed__l

Reputation: 1336

want to remove all the symbols(.,) and just keep the numbers

Using regular expression:

String exprStr="9.555.555,00";
exprStr=exprStr.replaceAll("[.,]","");
System.out.println(exprStr);

Where [.,] is the regular expression identifying either . or ,

Upvotes: 0

radimpe
radimpe

Reputation: 3215

dotIndex = tempBalance.indexOf(j+1, '.');

Should instead be

dotIndex = tempBalance.indexOf('.', j+1);

But that is not the only problem. Once you have the '.' fixed. You still need to sort out parsing all the ',' out as well. Simply fixing the above will still only return 9955458 minus the 23

Upvotes: 3

Saj
Saj

Reputation: 18712

To remove non-digit characters, you can do-

public static String removeNonDigits(String input) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (Character.isDigit(c)) {
            result.append(c);
        }
    }
    return result.toString();
}

Also, for formatting number you can use NumberFormat with Locale.

Upvotes: 1

yair
yair

Reputation: 9245

You should switch argument places in indexOf:

dotIndex = tempBalance.indexOf('.', j + 1);

First argument is the character to search, second is the index to start with.

Upvotes: 2

Related Questions