0

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

1
  • 1
    Use replace() instead of replaceAll(). Their names are really ambiguous, they should use replaceAll() for replace() and replaceRegex() for replaceAll(). Commented Mar 6, 2013 at 13:23

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Your program is using fileinput as a String, not as an array[][]. Now the output is just replacement of A-G with R. My desired output is R if a column contains A-G, and a space if a column does not contain one instance of A-G. So I still need a real solution.
Check my edit.. and please rate and accept if this helps cheers
Your solution works. Thank you. But when I'm trying to use the code in a greater program, I get a java.lang.NullPointerException when it calls matrix[j][i]. Do you have any idea what I am doing wrong?
Use your IDE debugger to follow through your Code Execution. Please upvote too :) .. cheers
1

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

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.