user1189952
user1189952

Reputation: 157

Convert 2D array to 1D array of column strings

I'm working on a java program that has the following input:

A V V V VV V V V
E F V E VF E V E
C D V C VD B V C
A A V A VA G V A
V D V V VD E V V
A V V A VV V V A

and the following output:

R
R

R
 R
R

R

So an R has to be printed if a column contains minimal one letter A-G, but a space has to be printed if there are only V's in the column. The code I have is:

Scanner inputc2 = new Scanner (new File("internalc"));
int sss = 0;

for (int i = 0; i < n; i++) {
 for (int j = 0; j < m; j++) {
  try{
     matrix2[i][j] = inputc2.useDelimiter(" ").next().replaceAll(",", " ");
       System.out.println(matrix2[i][j]);
       temp2[sss] = matrix2[i][j];//change this

        if(temp2[sss].indexOf("N") >= 0){
         pc2.println(temp2[sss].replaceAll("N","R"));
        }
        if(temp2[sss].indexOf(' ') >= 0){
         pc2.println(temp2[sss]);
        } 
        sss++;

     }
     catch (java.util.NoSuchElementException e) {
         // e.printStackTrace();
     }
   }

}

In the current program the temp2[sss] is exactly the same as matrix2[i][j], but it must be strings of columns. I hope that you can help me.

Kind regards, Bjorn

Upvotes: 0

Views: 613

Answers (2)

Sudhakar
Sudhakar

Reputation: 4873

Heres a sample program i wrote to accomplish, the output may not be exactly as you expect , well adapt the regex expression to your needs

public static void main(String[] args) throws Exception {

    Pattern pattern = Pattern.compile("[A-G]+[A-G]{0,1}");
    Pattern pattern2 = Pattern.compile("[V]+[V]*");

    BufferedReader br = new BufferedReader(new FileReader(new File("test.txt")));
    String line;
    while ((line = br.readLine()) != null) {
        StringBuffer sf = new StringBuffer();

        //System.out.println(line);         
        String[] tempArr = line.split(" ");
        //System.out.println(tempArr.length);

        for(String s : tempArr){
            Matcher matcher = pattern.matcher(s);
            Matcher matcher2 = pattern2.matcher(s);
            if (matcher.find()) {
                sf.append("R");
            }else if(matcher2.find())   {
                sf.append(" ");
            }
        }
        System.out.println(sf);
    }
    br.close();
}

Edit 2

There you go

public static void main(String[] args) throws Exception {

    Pattern pattern = Pattern.compile("[A-G]+");
    Pattern pattern2 = Pattern.compile("[V]+");

    List<String[]> mat = new ArrayList<String[]>();
    String[] row1 = { "A", "V", "V", "V", "VV", "V", "V", "V" };
    String[] row2 = { "E", "F", "V", "E", "VF", "E", "V", "E", };
    String[] row3 = { "C", "D", "V", "C", "VD", "B", "V", "C" };
    String[] row4 = { "A", "A", "V", "A", "VA", "G", "V", "A" };
    String[] row5 = { "V", "D", "V", "V", "VD", "E", "V", "V" };
    String[] row6 = { "A", "V", "V", "A", "VV", "V", "V", "A" };

    mat.add(row1);
    mat.add(row2);
    mat.add(row3);
    mat.add(row4);
    mat.add(row5);
    mat.add(row6);

    int rowSize = 6;
    int colSize = 8;

    String[][] matrix = mat.toArray(new String[rowSize][colSize]);

    for (int i = 0; i < colSize; i++) {

        StringBuffer sf = new StringBuffer();
        for (int j = 0; j < rowSize; j++) {
            sf.append(matrix[j][i]);

        }

        Matcher matcher = pattern.matcher(sf.toString());
        Matcher matcher2 = pattern2.matcher(sf.toString());

        if (matcher.find()) {
            System.out.println("R");
        } else if (matcher2.matches()) {
            System.out.println(" ");
        }

    }

}

Result:

R
R

R
R
R

R

Upvotes: 1

Sunil Rk
Sunil Rk

Reputation: 1029

You can replace this line

temp2[sss] = matrix2[i][j];

by

List<Integer> temp2 = new ArrayList<Integer>();
temp2.add(matrix2[i][j]);
int[] vector = new int[temp2.size()];
for (int i = 0; i < vector.length; i++) {
    vector[i] = temp2.get(i);
}

Upvotes: 1

Related Questions