0

I'm trying to convert array of numbers to array of words for example:

{1,2} will converted to {"one","two"}

so this is the code that I write in java:

public static void main(String[] args) {

    ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(1,2));

    Integer [] list1Array = list1.toArray(new Integer[0]);
    int numLength2 = list1.size();
    for(int i = 0; i < numLength2; i++){
        System.out.println(list1Array[i]);
     }   
    System.out.println(numLength2);
    String n2 = ""; 
    for(int j = 0; j < numLength2; j++) {
       int element = list1.get(j);
       System.out.println(element);
       switch (element) {
              case '1': {  
                    n2 = n2 + "one";
                    break;
                }
              case '2': { 
                    n2 = n2 + "two";
                    break;
                }

                default: {
                    n2 = n2 + "zero";
                }
            }
        }
        System.out.println(n2);
    }
}

It works fine except the last print:

 System.out.println(n2);

The output here is zerozero while it should be onetwo. What's problem with code?

4 Answers 4

5

element is an int, but you're comparing it to a char literal. You should use int literals instead:

switch (element) {

    case 1: {  
        n2 = n2 + "one";

        break;
    }
    case 2: { 
        n2 = n2 + "two";
        break;
    }

    default: {
        n2 = n2 + "zero";
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@GNDevs If this solved your issue, please do consider selecting it as the Accepted Answer.
I will but there is a note says that I should wait 3 minutes until I can accept the answer @Mike Bruesch
@Mureinik sorry but in the question I said that I tried to convert numbers array to words array too and the result now is string so can I make the it array like{"one","two"}?
1

case '1' , '1' is char type but element is int

Comments

1

Also, if you change this line and the program will work:

int element = '0' + list1.get(j);

Comments

1

You could have achieved same through these few lines of codes instead of your forloop and switch.

public static void main(String[] args) {
    List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 5, 4));
    String[] numArray = { "zero", "one", "two", "three", "four", "five" };

    for (Integer num : list1) {
        System.out.print(numArray[num]);
    }
}

This prints,

onetwofivefour

Now as you said you want to create a list like {"one", "two"}, you can do something like this,

public static void main(String[] args) {
    List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 5, 4));
    String[] numArray = { "zero", "one", "two", "three", "four", "five" };
    List<String> wordList = new LinkedList<String>();

    for (Integer num : list1) {
        wordList.add(numArray[num]);
    }
    System.out.println(wordList); // prints [one, two, five, four]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.