-1

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.

  • Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – fantaghirocco Nov 18 '22 at 13:29
  • `myArr` is out of [scope](https://www.baeldung.com/java-variable-scope) in `public static void main` and an array in Java does not expose an `add` method – fantaghirocco Nov 18 '22 at 13:36
  • how do I update the array with new values then – kelkrekeksa Nov 18 '22 at 13:40
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – fantaghirocco Nov 18 '22 at 13:41
  • ... but you should consider to use a [list](https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html) instead in my opinion - since an array has a fixed size – fantaghirocco Nov 18 '22 at 13:44
  • I tried fill() too but that doesn't seem to work either. Should I use a different way to store and print the solution instead of an array? – kelkrekeksa Nov 18 '22 at 13:50

1 Answers1

0

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("The word 'Software' converted to a number is " + getPhoneNumber("Software"));
    }
pfurbacher
  • 1,789
  • 3
  • 15
  • 23
  • Note that this approach of converting a string of digits to an int is fragile. If the word cannot be converted to an int (i.e., the number would be larger than Integer.maxInt), a ```java.lang.NumberFormatException``` will be thrown. If you really need to convert a word (String) to any number of digits in length, consider using a String to represent those digits. – pfurbacher Nov 18 '22 at 21:26
  • Sorry, I am new to coding and I don't understand what the -> is used for and how getNumber( (char) ch) interacts with it, could you explain it please? – kelkrekeksa Nov 21 '22 at 07:03
  • Since Java 14, the "case L ->" label can be used, eliminating the "break". After the word "case" one to many constants of the same type are allowed: i.e., ```case 'A', 'B', and so on as shown in the code above. The "->" means return the value of the expression to the right from the function, without evaluating any of the remaining case labels. – pfurbacher Nov 22 '22 at 04:37
  • You also asked about ```getNumber((char) ch)```. It is a method which is called in the body of the ```getPhoneNumber(String word)``` method. In programming, you should aim to write small methods (other languages call them functions) which do a well-defined task. In the above code, when you run the program, the ```main(...)``` method is called. In it's body is a call to ```getPhoneNumber(...)```. That method, in turn, calls ```getPhoneNumber(...)```. In this example, each method (except ```main(...)``` returns a value to its caller. – pfurbacher Nov 22 '22 at 04:48
  • Finally, you might want to look into a free online Java course: https://medium.com/javarevisited/10-free-courses-to-learn-java-in-2019-22d1f33a3915 – pfurbacher Nov 22 '22 at 04:48