user3055121
user3055121

Reputation: 13

how to find decimal dot

I have problem searching the position of decimal dot in my char array.

I transformed double (Price) into char array using:

char[] arrayPrice=Double.toString(Ad[i].Price()).toCharArray();

Now I want to find the position of decimal dot.

I tried

for (int k=0; k<arrayPrice.length; k++) {
    if (".".equals(arrayPrice[k])) {
        dotPosition=k;
    }
}

but it gives me 0 all the time.

Thanks!

Upvotes: 1

Views: 64

Answers (2)

nachokk
nachokk

Reputation: 14413

Well you are comparing different types, this will always be false.

".".equals(arrayPrice[k])

Instead you can use

'.' == arrayPrice[k]

But String class provide some utils method for doing that, for example String#indexOf(..)

Returns the index within this string of the first occurrence of the specified character

String price=Double.toString(Ad[i].Price());
price.indexOf('.');

Upvotes: 3

Aniruddha Sarkar
Aniruddha Sarkar

Reputation: 485

A "." acts as a wildcard. so instead of using

  ".".equals(arrayPrice[k])

you may use

  '.'==arrayPrice[k]

in the if statement. This will surely fix the problem.

Upvotes: 1

Related Questions