'How to change a number written in a string form to an integer number
This is the code which I thought is supposed to work but it just returns garbage in the array I think and I've been trying to fix it but I can't find the error.
Thought the code is simple initially I want to input a string which has written numbers and return those numbers as digits like : "two one nine six eight one six four six zero", "2196816460"
private static final String[] numNames = {"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",};
private static final char[] nums = {'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',};
public static String getPhoneNumber(String s) {
int k = 0;
int z = 0;
char [] chararray3 = new char [10];
char [] chararray = s.toCharArray();
for (int i = 0; i < chararray.length; i++){
if (chararray[i] == ' '){
char [] chararray2 = new char[i-k-1];
for (int j = 0; j < i - k; j++){
chararray2 [j] = chararray[k];
k++;
}
k++;
for (int x = 0; x < 9; x++){
if (chararray2.toString() == numNames[x]){
chararray3 [z] = nums[x];
z++;
}
}
}
}
return chararray3.toString();
}
Solution 1:[1]
First issue is that
[].toString()
doesn't display the content of it, you needArrays.toString(chararray3)
Then, your condition to do things in the loop is
if (chararray[i] == ' ')
, as there is no space, nothing execute, in fact, you are just creating an empty array, then return it, no more.Your code is quite complex, and can't see how to "fix" it, so I propose a simpler way
Build a
String []
to save the names (can't be achar[]
)For each char of the input:
- find their position in
nums
- save their corresponding value in
numNames
into yourresult
- find their position in
public static String getPhoneNumber(String s) { char[] chararray = s.toCharArray(); String[] result = new String[s.length()]; for (int i = 0; i < chararray.length; i++) { if (chararray[i] != ' ') { for (int j = 0; j < nums.length; j++) { if (chararray[i] == nums[j]) { result[i] = numNames[j]; break; } } } } return Arrays.toString(result); }
Note that "0"
can be to get value at index 0
, no need of nums
public static String getPhoneNumber(String s) {
String[] values = s.split("");
for (int i = 0; i < values.length; i++) {
if (!values[i].isEmpty()) {
values[i] = numNames[Integer.parseInt(values[i])];
}
}
return Arrays.toString(values);
}
Solution 2:[2]
A simple solution using Stream API:
public static String getPhoneNumber(String s){
// decorate the array as a list (no copying involved!) so we can have indexOf
final List<String> numNamesList = Arrays.asList(numNames);
// Split, transform, join
return Arrays.stream(s.split("[\s]+"))
.map(numNamesList::indexOf) // we assume all names exist!
.map(index -> String.valueOf(nums[index]))
.collect(Collectors.joining());
}
Solution 3:[3]
So it was too complex indeed so I came up with a different code :
public static String getPhoneNumber(String s) {
int k = 0;
String nums = "";
String[] StringArray = new String[10];
char [] result = new char[10];
for (int y = 0; y < StringArray.length; y++) {
StringArray[y] = "";
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
k++;
continue;
}
StringArray[k] += s.charAt(i);
}
for(int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (StringArray[i].equals(numNames[j])) {
nums += numer[j];
}
}
}
return nums;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Michail Alexakis |
Solution 3 | Abra |