Reputation: 25
If I input a String like "-32323" my out put is [-50,-51,-50,-51,-50], and I am not sure where these values are coming from. The output I am trying to get is [-3,-2,-3,-2,-3]
static LinkedList<Integer> list = new LinkedList<Integer>();
public static LinkedList<Integer> method(String s)
{
char[] listOfChar;
int[] num;
list = new LinkedList<Integer>();
listOfChar = s.toCharArray();
if (listOfChar[0] == '-') {
num = new int[listOfChar.length - 1];
for (int i = 0; i < num.length; i++) {
num[i] = -1 * listOfChar[i + 1];
}
}
else {
num = new int[listOfChar.length];
for (int i = 0; i < num.length; i++) {
num[i] = listOfChar[i];
}
}
for (Integer x : num) {
list.push(x);
}
return list;
}
Upvotes: 0
Views: 122
Reputation: 26198
problem:
listOfChar[i + 1];
It will convert your char
to decimal value
of it (ex. char 3
is equals to 51
).
click here to know the decimal values of different characters
solution:
num[i] = -1 * (listOfChar[i + 1] - '0');
You need to deduct char 0
to make it 3
, because char 0
in decimal is 48
which means (51 - 48
) is equals to 3
.
Upvotes: 3