Say, I have an array similar to this,
{"A", "1", "B", "2", "C", "3"}
I want to put it to HashMap
like pairs, I tried few ways, but none seem to work.
I created a method isDigit() to check if an item is digit,
private static boolean isDigit(String str){
try {
Integer.parseInt(str);
}
catch (NumberFormatException nfe){
return false;
}
return true;
}
Then I tried separating into 2 arrays, numbers and letters.
for (int i = 0; i <= parts.length; i++) {
if (isDigit(parts[i])) {
numbers[i] = parts[i];
} else {
abcs[i] = parts[i];
}
}
and finally,
for (int i = 0; i < numbers.length ; i++) {
map.put(abcs[i], numbers[i]);
}
and I print them with,
for (String each: map.keySet()) {
System.out.println(each + ":" + map.get(each));
}
which prints something like,
1
A : null
2
B : null
What it should print is,
A : 1
B : 2
C : 3