Tomeek
Tomeek

Reputation: 3

How to get an element's int value from a String matrix?

I would like to get an element's int value from a String matrix.

My task is to implement the easiest version of Uniqual (https://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/unequal.html) with some kind of tree or graph search algorithm.

That's why I thought of implementing the table as a string, as it can contain both characters and digits.

I have already tried this method: Character.isDigit(matrix[i].charAt(j)) for my issue. I guess it's not the correct way to do so. Maybe if I get the element's char value, I could implement a simple function that checks whether it is a number or a digit or special char.

Upvotes: 0

Views: 133

Answers (2)

a13e
a13e

Reputation: 928

I think this is what you are looking for -

char c = matrix[i].charAt(j);
int cVal = Character.getNumericValue(c);

I am preferring this method because if c='1', cVal = 1.

Upvotes: 1

ControlAltDel
ControlAltDel

Reputation: 35096

Character.isDigit(matrix[i].charAt(j)) just tells you if the character is a digit character. To get the value from the character, simply subtract 0's character value

int val = (int) (matrix[i].charAt(j) - (int)'0');

Upvotes: 0

Related Questions