0

I need to make a 2D array from a given String. A number of rows will be a number of words in String and the number of columns will be the length of the longest word in String (words are separated with space). If in the String there is a 2 letter word (for example "is") and the column number will be 8 I need to fill the rest of the cells with space.

Example:

String: "aaa bbbb ccccc dddddd"

Array: stringArray[4][6]

Output: {{a,a,a,' ',' ',' '},{b,b,b,b,' ',' '},{c,c,c,c,c,' '},{d,d,d,d,d,d}}

The code is:

public static void TextToArray(String s) {
        int rowCount = 0;
        int columnCount = 0;
        int tempColumn = 0;

        //getting array dimensions and creating an array

        for (int i = 0; i < s.length(); i++) {
            tempColumn++;
            if (s.charAt(i) == ' ') {
                rowCount++;
                if (tempColumn > columnCount) {
                    columnCount = tempColumn;
                }
                tempColumn = 0;
            }
        }

        char[][] stringArray = new char[rowCount + 1][tempColumn];

        //filling array - not completed

        for (int i = 0; i < stringArray.length ; i++) {
            for (int j = 0; j < stringArray[i].length ; j++) {
                if(s.charAt(j) == ' '){
                    for (int k = j; k < stringArray[i].length - j ; k++) {
                        stringArray[i][j] = ' ';
                    }
                }
                stringArray[i][j] = s.charAt(j);
                System.out.print(stringArray[i][j] + ", ");
            }
            System.out.println();
        }

I have a problem with inserting spaces to empty cells (i have no idea - i need some pointers to make it). Im also stuck on place where j loop is ending and starting from the beggining (the letters that loop is inserting to array starts from the beginning of the string - i have no clue where to instert condition to continiue iterating through String length). I know that the second part of method is bad but i need help to end it.

2 Answers 2

2

First, you split the String by space as delimiter, so you get your rowcount. Then you search for the longest substring to get your columncount. After that you can insert them rowwise, where a row n is equal to your n-th position in the tmp array. The if statement fills every cell behind the word(if the word is not the longest in the input String) with ' '.

public static void TextToArray(String s) {
    String[] tmp = s.split(" ");

    int rowCount = tmp.length;
    int columnCount = 0;

    for(int i = 0;i < tmp.length; i++) {
        if(tmp[i].length() > columnCount) {
            columnCount = tmp[i].length();
        }
    }

    char[][] charArray = new char[rowCount][columnCount];

    for(int j = 0; j < rowCount; j ++) {
        for(int k = 0;k<columnCount;k++) {
            if(k<tmp[j].length()) {
                charArray[j][k] = tmp[j].charAt(k);
            }
            else {
                charArray[j][k] = ' ';
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Let me propose you a similar algorithm using a list instead of an array of string at the first step to manipulate little bit functionnal programming

public class Main {
  public static void main(String[] args) {
    String s = "aaa bbbb ccccc dddddd";
    char[][] stringArray = text2array(s);
    for (int i = 0; i < stringArray.length ; i++) 
        for (int j = 0; j < stringArray[i].length ; j++)
            System.out.print(stringArray[i][j]);
  }

  public static char[][] text2array (String st) {
    List<String> list = Arrays.asList(st.split(" "));
    Optional<String> wordMax = list.stream().reduce((word1, word2) -> word1.length() > word2.length() ? word1 : word2); 
    int maxLength = 0;
    if(wordMax.isPresent()) {
        maxLength = wordMax.get().length();
    }
    char[][] array = new char[list.size()][maxLength];
    for (int line = 0; line < list.size(); line++) {
        for (int col = 0; col < maxLength; col++) {
            String word = list.get(line);
            if (col < word.length()) array[line][col] = word.charAt(col);
            else array[line][col] = ' ';
        };
    }
    return array;
}

}

output :     aaa   bbbb  ccccc dddddd

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.