What is best suit to start the switch statement?

import java.util.*;
import java.lang.*;

public class Order {
  public static String order(String words) {
    // ...
    /*Enumeration in=words.elements();
    
    while (in.hasMoreElements()) 
        { 
            // moving cursor to next element 
            char i =(char)e.nextElement(); 
            
            if(i=='1'||i==)*/
            String[] p= new String[100];
  String[] arrOfStr = words.split(" ", 2); 
  
        for (String a : arrOfStr) 
           {
           
              switch()
              {
                case a.contains("0"):p[0]=a;
                                     break;
                case a.contains("1"):p[1]=a;
                                     break;
                case a.contains("2"):p[2]=a;
                                     break;
                case a.contains("3"):p[3]=a;
                                     break;
                case a.contains("4"):p[4]=a;
                                     break;
                case a.contains("5"):p[5]=a;
                                     break;
                case a.contains("6"):p[6]=a;
                                     break;
                case a.contains("7"):p[7]=a;
                                     break;
                case a.contains("8"):p[8]=a;
                                     break;  
                case a.contains("9"):p[9]=a;
                                     break;                     
              }
                 
           
            }
      return p;       
    
  }
}

i don’t understand what is best suit for this switch case,
i am trying to solve this problem in java,but can’t solve for this switch.
my logic is to check the word of a sentence contains a numbers 0-9 and set the words sequentialy.so i thought switch case may be best for this work.

link of the problem is:https://www.codewars.com/kata/your-order-please/train/java

you can check the problem statement.

Hi @darkdebo, there are few things that I would like to point out.

From what I understand from the challange, you’ll always have at most 9 words in the initial string variable.

So I would set the empty String array to a length of 9 and not of 100.
Secondly I would put 0 as the second parameter of the String.split method, otherwise you’ll split the intial string only twice (you can check the doc here).

Than I think you need to put a default case anyway in the switch.

And lastly you should return a String and not an array, which means you have to find a way to reconstruct the final String from the ones included in the array.

After these modifications I think it should work.

PS: also note that the challanges says the numbers start from 1 and go to 9, so you’ll never have a word with 0 in it.
Which means the case a.contains("0") is never verified and p[0] is null.
Either start to iterate the new array from index 1 when building the String or assign the String a to an index less than 1 compared to the number found (case a.contains("1"):p[0]=a;)

Happy coding :slight_smile:

Ok try it sir.thank you for reply.

import java.util.*;
import java.lang.*;

public class Order {
  public static String order(String words) {
    // ...
    /*Enumeration in=words.elements();
    
    while (in.hasMoreElements()) 
        { 
            // moving cursor to next element 
            char i =(char)e.nextElement(); 
            
            if(i=='1'||i==)*/
            String[] p= new String[9];
  String[] arrOfStr = words.split(" ", 0); 
  
        for (String a : arrOfStr) 
           {
           
              switch(a)
              {
                case a.contains("1"):p[1]=a;
                                     break;
                case a.contains("2"):p[2]=a;
                                     break;
                case a.contains("3"):p[3]=a;
                                     break;
                case a.contains("4"):p[4]=a;
                                     break;
                case a.contains("5"):p[5]=a;
                                     break;
                case a.contains("6"):p[6]=a;
                                     break;
                case a.contains("7"):p[7]=a;
                                     break;
                case a.contains("8"):p[8]=a;
                                     break;  
                case a.contains("9"):p[9]=a;
                                     break;
                default:break;                                         
              }
                 
           
            }
      String k="" ;     
      for(int i=1;i<=p.length;i++)   
      {
        k+=p[i];
      }   
    return k;
  }
}

After modifying I also face this type of error
Type mismatch.

@simonebogni

Didn’t notice it earlier.
Before the switch starts you can check the string and extract the number from it (either as int or as chat/String).
As the switch parameter put the number you have found.
Than in every case, instead of the Boolean (which is not the correct use of a switch-case statement), put the number you are testing (case 3: ... or case '3':... or case "3":... depending on the type of the variable you are testing).

Edit:

A switch works with the byte , short , char , and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character , Byte , Short , and Integer

From the Java switch oracle docs.

import java.util.*;

public class Order {
  public static String order(String words) {
    // ...
    /*Enumeration in=words.elements();
    
    while (in.hasMoreElements()) 
        { 
            // moving cursor to next element 
            char i =(char)e.nextElement(); 
            
            if(i=='1'||i==)*/
            String[] p= new String[9];
  String[] arrOfStr = words.split(" ", 0); 
  
        for (int i=1;i<=arrOfStr.length;i++) 
           {
           
              switch(i)
              {
                case 1:                
                
                arrOfStr[i].contains("1");
                p[1]=arrOfStr[i];
                                     
                                     
                                     break;
                case  2:arrOfStr[i].contains("2");
                       p[2]=arrOfStr[i];
                                     
                                     break;
                case 3:arrOfStr[i].contains("3");
                       p[3]=arrOfStr[i];
                                     
                                     break;
                case 4:arrOfStr[i].contains("4");
                       p[4]=arrOfStr[i];
                                     break;
                                     
                case 5:arrOfStr[i].contains("5");
                       p[5]=arrOfStr[i];
                                     
               
                                     break;                     
                                           
                case 6:arrOfStr[i].contains("6");
                       p[6]=arrOfStr[i];
                                     
                                     break;
               
                                                                                            
                default:break;                                         
              }
                 
           
            }
      String k="" ;     
      for(int i=1;i<=p.length;i++)   
      {
        k+=p[i];
      }   
    return k;
  }
}

After some modifications all error gone,but I face stack trace.why I got this.

Where are case 7, 8 and 9?
Also could you post the error you get?

I got stack trace error and I put other case after solving stack trace error.

Saying that you got a “stack trace error” doesn’t realy help as in it doesn’t say anything specific to the problem. It could be that you got there for any kind of unhandled exceptions.

From https://programming.guide/java/stack-trace.html

A stack trace is the list of methods that the program was in the middle of when the stack trace was printed. It’s typically printed to the console when an unexpected error occurs (an exception is thrown but never caught). A stack trace is very useful for debugging: not only do you see where the error happened, but also how the program arrived in this place.

Basically what I was asking for, was this message:


Which shows you have an ArrayIndexOutOfBoundsException.
That happens because you are trying to put an element in a position that is not present in the array.
Remember that the array start at index 0 and not one.
So in the stringArray (not the result array) you need to start to iterate from the index 0:
for (int i=0;i<=arrOfStr.length;i++) instead of for (int i=1;i<=arrOfStr.length;i++)
Because otherwise in the case of a 4-index array (0, 1, 2, 3), you end up trying to access to the positions 1, 2, 3 and 4. When you get to number 4, which does not exist, the Exception is thrown.

Edit: also note that you have modified your code from
for (String a : arrOfStr) (which doesn’t create the problem)
to : for (int i=1;i<=arrOfStr.length;i++)

Also the problem is still not solved because by doing so, you are not extracting the number from the string to check in which order the words have to be put.
What the code is doing, once you get rid of the exception, is:
Split the long string in words and put them in an array.
Take each word and check its index in the array. Than go to case identical to the index.
If that word contains said number, put it in the new array, otherwise it won’t go there and it will never be checked again (because on the next iteration the index increases).
Even if the initial index in the for loop is brought to 0, this code will work only if the words are already ordered from 1 to 9 in the initial string.

You need to extract the number from the string before the switch statement start and apply the relative case based on it.

1 Like

Ok I do it and post it here.