0

I want to convert one dimensional array of Strings into two dimensional array

of number of rows equal to number of array's elements.

the one dimensional array has many elements so each element should be on a row

after splitting itself into nine element.

But the output is one dimensional array in addition to exceptions java.lang.ArrayIndexOutOfBoundsException 9

Can anyone help?

public class StringFragementation

{

public static  String [][]  mymethod(String [] mystring)
    {

         int row = 0, col = 0;

        String[][] india = new String[2][9];

        String mystringno2[];

        mystringno2 = mystring;

        int i = 0;

        int j = 0;

        String x = "_";

        int i1;

       String Nahda="";

      int l;

      for( l=0;l<mystringno2.length;l++)

      {

     Nahda =  Nahda.concat(mystringno2[l]);

      }

      System.out.println( Nahda );

      i1 =0;

     while (j <Nahda.length()) 

     {

      i = Nahda.indexOf(x, i);

      i1 = i + 1;

      i1 = Nahda.indexOf(x, i1);

     if (i1 >Nahda.length())

     {

      break;

      }

      i++;

     india[row][col] = Nahda.substring(i, i1);

     System.out.print("Element  " + row + "  " + col + "   " + india[row][col]+"\t");

     col++;

     }

    return india;

     }

public static void main(String[] args)

{

String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"};

String [][] singapore = new String[s.length][9];

singapore = mymethod(s);

 for(int col=0,k=0;col <9;col++)

   for(int row=0;row<s.length;row++) 

   {
     System.out.print(  singapore[row][col++]+"\t"   )   ;

   }

    System.out.println("\n");

}

}

3 Answers 3

1

The problem is that indexOf() returns -1 if the string is not found. Now because your Strings with the country names have no "_" at the very end the indexOf function will return -1. Therefore your loop will not exit and col will become 9 which causes an IndexOutOfBoundsException.

I might be wrong but try it with adding a "_" at the very end.

Also you never increase row in your loop so that's why there's only one dimension in the array returned.

EDIT

Ok now I get it, so you have 18 countries and want to arrange them on 2 rows with 9 columns. Well you never increase row so that won't work.

Try this:

public class StringFragementation

{

public static  String [][]  mymethod(String [] mystring)
{

    int row = 0, col = 0;

    String[][] india = new String[2][9];

    String mystringno2[];

    mystringno2 = mystring;

    int i = 0;

    int j = 0;

    String x = "_";

    int i1;

    String Nahda="";

    int l;

    for( l=0;l<mystringno2.length;l++)

    {

        Nahda =  Nahda.concat(mystringno2[l]);

    }

    System.out.println( Nahda );

    i1 =0;

    // set i before the loop
    i = Nahda.indexOf(x, i); 

    while (j <Nahda.length()) {

        i1 = Nahda.indexOf(x, i + 1);

        if (i1 == -1) { 
            // No more "_" we're at the end but we still need to add the last country!
            india[row][col] = Nahda.substring(i, Nahda.length() - 1);
            break;
        }

        india[row][col] = Nahda.substring(i + 1, i1);

        // set i to the "_" after the current country.
        i = i1; 

        System.out.print("Element  " + row + "  " + col + "   " + india[row][col].toString()+"\n");

        col++;

        if (col >= 9) { 
            // we're at the end of the first row, go to second row
            col = 0;
            row++;
        }

    }

    return india;

}

public static void main(String[] args)

{

    String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"};

    String [][] singapore = new String[s.length][9];

    singapore = mymethod(s);

    for(int col=0; col < 9;col++)

        for(int row=0;row<s.length;row++) 

        {
            System.out.print(  singapore[row][col]+"\t"   )   ;

        }

    System.out.println("\n");

}

}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried increase row in my loop but unfortunately that doesn;t ]]
I just tried it myself, it works.. hang on I will post the whole class in my answer above.
@ElhadiMamoun So you deleted your comment, is it working now?
it working but why it doesn't print the elements , and why it gives exception?[code]_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia__Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria_ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at StringFragementation.mymethod(StringFragementation.java:55) at StringFragementation.main(StringFragementation.java:80) Element 0 0 Element 0 1 Element 0 2 Element 0 3 Element 0 4 Element 0 5 Element 0 6 Element 0 7 Element 0 8 Element 1 0 ........etc
@ElhadiMamoun Yes I have found the error.. check my answer again I post the whole class, this is working now! :)
1

Here is the fixed version of your code:

public class StringFragementation

{

public static  String [][]  mymethod(String [] mystring)
    {

         int row = 0, col = 0;

        String[][] india = new String[2][9];

        String mystringno2[];

        mystringno2 = mystring;

        int i = 0;

        int j = 0;

        String x = "_";

        int i1;

       String Nahda="";

      int l;

      for( l=0;l<mystringno2.length;l++)

      {

     Nahda =  Nahda.concat(mystringno2[l]);

      }

      System.out.println( Nahda );

      i1 =0;

     while (j <Nahda.length()) 

     {

      i = Nahda.indexOf(x, i);

      i1 = i + 1;

      i1 = Nahda.indexOf(x, i1);

     if (i1 <0)

     {

      break;

      }

      i++;

     india[row][col] = Nahda.substring(i, i1);

     System.out.print("Element  " + row + "  " + col + "   " + india[row][col]+"\n");

     col++;
         if (col > 8) {
             row++;
             col=0;
         }
     }

    return india;

     }

public static void main(String[] args)

{

String[] s ={"_Netherlands_Iceland_Norway_Denmark_Usa_Brazil_Argentina_Colombuia_Bolivia","_Belgium_Romania_France_Germany_Switherland_Russia_Sweden_Italy_Austria"};

String [][] singapore = new String[s.length][9];

singapore = mymethod(s);

 for(int col=0,k=0;col <9;col++)

   for(int row=0;row<s.length;row++) 

   {
     System.out.print(  singapore[row][col++]+"\t"   )   ;

   }

    System.out.println("\n");

}

}

you forgot to switch to the new 2D array(count the number of columns and if it's over 8 increase the column and reset the row to 0, also you forgot that indexOf returns -1 if nothing is found so i fixed that also

Comments

0

I suggest you split the String in to tokens rather than taking subSting of it. It more looks like a C-style solution to this type of question. How about this?

    public static String[][] mymethod(String[] input) {
        String[][] result = new String[input.length][];
        for(int i = 0 ; i < input.length ; i++) {
            List<String> temp = Arrays.asList(input[i].split("_"));
            temp = temp.subList(1, temp.size());
            String[] row = new String[temp.size()];
            temp.toArray(row);
            result[i] = row;
        }
        return result;
    }

Hope this helps.

3 Comments

The problem is that there's a _ at the beginning of the string already, I'm not sure if he can change the input.
If you split by _, the first _ will cause the first generated token to be empty string. It is removed above using the line: temp = temp.subList(1, temp.size()); So it should be fine.
oh, didn't see the subList :) great idea.

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.