0

I'm having trouble with this, maybe you could help me: I have 3 strings like: word1, word2, word3 and I have to build a matrix with them, like this: on the first row : word1("ABC"), second row: word2("DEF") and third row: word3("GHI").

A|B|C
D|E|F
G|H|I

I need this because after that I have to check if the formed words ("ADG","BEH","CFI") are in an array of words. And I don't know how to put those strings in the matrix so I can check. Any help is useful. Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
OanaM
  • 85
  • 7

5 Answers5

1

Based on this comment:

the words have the same size, because the matrix is actually like a puzzle. I choose randomly 3 words from an array, put them in a matrix and check after that if the words resulted are from the same array.

I'll assume some things in order to make this work (since we don't have enough info):

  1. You have an array of Strings where you have all the words

    private String[] words;
    
  2. You have a method to randomly pick up 3 Strings from this array.

    private String s1, s2, s3;
    
    public void pickThreeRandomWords() {
        s1 = aRandomWord(words);
        s2 = aRandomWord(words);
        s3 = aRandomWord(words);
        //or maybe another fancy algorithm to get this...
    }
    

So you would need an array of array of chars based on these 3 Strings. This code could do the work for you:

public char[][] createMatrixFromStrings(String s1, String s2, String s3) {
    char[][] theMatrix = new char[3][]; //yes, hardcoded
    theMatrix[0] = s1.toCharArray();
    theMatrix[1] = s2.toCharArray();
    theMatrix[2] = s3.toCharArray();
    return theMatrix;
}

Of course, if you would want to make this method to support more than 3 Strings you can make the method to receive a random quantity of Strings:

public char[][] createMatrixFromStrings(String ... strings) {
    if (strings == null || strings.length == 0) return null;
    char[][] theMatrix = new char[strings.length][];
    int i = 0;
    for(String s : strings) {
        theMatrix[i++] = s.toCharArray();
    }
    return theMatrix;
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

You can build the result words without a matrix:

List<String> verticalWords = new ArrayList<String>();
for (int i = 0; i < horizontalLen; i++){
    String currentWord = "";
    for (int j = 0; j < wordCount; j++)
        currentWord += words.get(j).get(i);

    verticalWords.add(currentWord);
}

P.S. For the currentWord you can use a StringBuilder to make it more efficient, but I doubt it is highly needed here.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • Actually you don't need to explicitly put in a StringBuilder, that's an optimization the compiler is already doing for you. – Francisco Paulo Apr 20 '13 at 14:42
  • I doubt so. The optimization is done for me when I write: "ABC" + "DEF", because the compiler sees the constants. It is only for constants. – Petar Minchev Apr 20 '13 at 14:43
  • Well, I created a quick example class with your code and according to javap -v, there's a StringBuilder in there. Maybe it has to do with compiler settings, though I haven't touched mine. – Francisco Paulo Apr 20 '13 at 14:46
  • Yes, but a new StringBuilder is created EVERY time, on each concatenation. Read here - http://stackoverflow.com/questions/2721998/how-java-do-the-string-concatenation-using/2722062#2722062 – Petar Minchev Apr 20 '13 at 15:18
0

Java doesn't have matrix.It has array of array

So,you can try this

List<char[]> lst=new ArrayList();//stores a list of char[]
lst.add(("ADC".toCharArray()));//adds array of characters i.e 'A','D','C'
lst.add(("DEF".toCharArray()));
lst.get(0)[0];//A
lst.get(1)[0];//D

Now you can iterate vertically

for(int i=0;i<lst.size();i++)temp+=lst.get(i)[0];

temp would have AD which you can now cross check with equals method

Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

The main thrust of this goal is that you're taking a one-dimensional value, and converting it into a two-dimensional value. There are many ways you can do this, but here are the two that come off the top of my head:

  • Set up a nested while loop to iterate over the first dimension, and when it reaches the length, reset and cause the outer loop to increment, much like a clock

  • You can create a new subarray using ArrayUtils.toSubArray(), and with some finagling, get that to work:

    • Create a new row of the array each time, based on the dimension slices you want to hit up. I'll leave figuring this one out as an exercise for the reader. But here's a hint:

      for(int i = 0; i < theDimension; i++, j += 3) {
          ret[i] = ArrayUtils.subarray(word, i*theDimension, j);
      }
      

Lastly, I assume that there's a restraint on the type of input you can receive. The matrix must be square, so I enforce that restriction before we build the array.

I strongly encourage you to poke and prod this answer, and not just blindly copy it into your schoolwork. Understand what it's doing so you can reproduce it when you're asked to again in the future.

public char[][] toMatrix(int theDimension, String theEntireWord) {
    if(theEntireWord.length() != theDimension * theDimension) {
        throw new IllegalArgumentException("impossible to add string to matrix of uneven dimension");
    }
    char[][] ret = new char[theDimension][theDimension];
    int i = 0;
    int j = 0;
    while(i < theDimension) {
        if(j == theDimension) {
            j = 0;
            ++i;
        } else {
            ret[i][j] = theEntireWord.charAt((i * theDimension) + j);
            j++;
        }
    }
    return ret;
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

I think this will sort your problem.

package printing;

public class Matrix {

public static void main(String[] args) {




    //Length can define as you wish




    String[] max = new String[10];




    String[] out = null;
    //Your Inputs




    max[0]="ADG";




    max[1]="BEH";




    max[2]="CFI";




    //following for loop iterate your inputs
    for (int i = 0; i < max.length; i++) {




        if(out==null){out= new String[max.length];}




        String string = max[i];




        if(string==null){ break;}




        //Here breaking input(words) one by one into letters for later contcatnating.
        String[] row = string.split("");




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




            String string1 = row[j];
          //  System.out.println(string1);




            //create the values for rows
           if(out[j]!=null){ out[j]=out[j]+string1;}




           else{




           out[j]=string1;
           }
        }
     }
    //following for loop will out put your matrix.
    for (int i = 0; i < out.length; i++) {
        String string = out[i];
        if(out[i]==null){break;}
        System.out.println(out[i]);

    }
}

}

Lasan
  • 183
  • 1
  • 4
  • 20