The thing I am trying to do is converting a word to numbers according to phone standart. The word will be inputted. An example would be the word "Software" becoming "76389273"
My plan was to convert the string into a list of characters and create a while loop including the switch function. My issue is that I have to store every returned value for every letter.
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
public class Phonething {
public static void ListTransform(String[] arg) {
Scanner input = new Scanner(System.in);
System.out.println("Enter word");
String word = input.nextLine();
char[] wordArray = word.toCharArray();
String wordList = Arrays.toString(wordArray);
System.out.println(wordList);
}
public static int main(int[] wordList, char t) {
int[] myArr = {};
int value = 0;
int i = 0;
char j = 0;
wordList[i] = j;
while ( i < wordList.length) {
myArr.add(value);
switch (j)
{
case 'A':
case 'B':
case 'C':
case 'a':
case 'b':
case 'c':
value = 2;
break;
case 'D':
case 'E':
case 'F':
case 'd':
case 'e':
case 'f':
value = 3;
break;
case 'G':
case 'H':
case 'I':
case 'g':
case 'h':
case 'i':
value = 4;
break;
case 'J':
case 'K':
case 'L':
case 'j':
case 'k':
case 'l':
value = 5;
break;
case 'M':
case 'N':
case 'O':
case 'm':
case 'n':
case 'o':
value = 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
case 'p':
case 'q':
case 'r':
case 's':
value = 7;
break;
case 'T':
case 'U':
case 'V':
case 't':
case 'u':
case 'v':
value = 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
case 'w':
case 'x':
case 'y':
case 'z':
value = 9;
break;
}
i ;
}
return value;
}
public static void main(String[] arg){
System.out.println(myArr);
}
}
I've tried creating an array and updating it by putting it in the while loop but the add operator is not working because of the error "cannot resolve method 'add(int)'". Another issue is that the final code System.out.println(myArr)
"gives the error cannot resolve symbol "'myArr'." That is why I can't print or update the final list.
CodePudding user response:
In main(String[] args)
, you don't call the method that does the conversion, oddly also called main()
.
Reworking your code, using the modern syntax for switch pattern matching (Java 14 ), and using the Java streaming API to iterate through the word (e.g., "Software") to create a string of numbers, and then converting that string to an int, here's a more succinct approach to converting a word to a number:
public static int getPhoneNumber(String word) {
return Integer.valueOf(word.chars()
.mapToObj(ch -> String.valueOf(getNumber((char) ch)))
.collect(Collectors.joining()));
}
public static int getNumber(char j) {
return switch (j) {
case 'A', 'B', 'C', 'a', 'b', 'c' -> 2;
case 'D', 'E', 'F', 'd', 'e', 'f' -> 3;
case 'G', 'H', 'I', 'g', 'h', 'i' -> 4;
case 'J', 'K', 'L', 'j', 'k', 'l' -> 5;
case 'M', 'N', 'O', 'm', 'n', 'o' -> 6;
case 'P', 'Q', 'R', 'S', 'p', 'q', 'r', 's' -> 7;
case 'T', 'U', 'V', 't', 'u', 'v' -> 8;
case 'W', 'X', 'Y', 'Z', 'w', 'x', 'y', 'z' -> 9;
default -> 0;
};
}
public static void main(String[] arg) {
System.out.println("Software -> " getPhoneNumber("Software"));
}
CodePudding user response:
I think you should consider using the unicode for this or statements like:
if (j>='A'&&j<='C'||j>='a'&&j<='c')
value=2;
this seems more efficient to me