PHPFan
PHPFan

Reputation: 796

Convert 1D String array to 2D array each element in row

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");

}

}

Upvotes: 0

Views: 1281

Answers (3)

Octoshape
Octoshape

Reputation: 1141

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");

}

}

Upvotes: 1

KKKCoder
KKKCoder

Reputation: 903

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.

Upvotes: 0

Goran Štuc
Goran Štuc

Reputation: 581

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

Upvotes: 1

Related Questions