1

--------------- SOLVED !!! ----------------------- I would like to thank you, everyone, for your help !

I have to read a file where I have nxm elements, and then put those elements into 2D array, and then print it out. I'm a little bit stuck at printing my array out. In file.txt I have 2x2 => 2 lines and 2 columns, and elements are:1 2 3 4.

Here is my code:

public class ex_1 
{
    public static void main(String args[])
    {   
        FileReader fr = new FileReader("FirstMatrix.txt");
        BufferedReader br = new BufferedReader(fr);
        String s = br.readLine();
        String[] split = s.split("x");
         int k=Integer.parseInt(split[0]);
         int l=Integer.parseInt(split[1]);
        System.out.println("Matrix dimensions: "+k+" lines,  "+l+" columns si "+k*l+" elements");

        System.out.print("Elements in matrix are: \n"); 

        int[][] FirstMatrix = new int [k][l];
                while ((s = br.readLine()) != null) 
                {       
                    for(int i=0; i<FirstMatrix.length; i++)
                        for(int j=0; j<FirstMatrix[i].length;j++)
                        {
                            FirstMatrix[i][j] = Integer.parseInt(s);
                            System.out.println("FirstMatrix["+i+"]["+j+"]="+FirstMatrix[i][j]);
                        }   
                }
                br.close();

My output, how it is:

FirstMatrix[0][0]=1   
FirstMatrix[0][1]=1    
FirstMatrix[1][0]=1    
FirstMatrix[1][1]=1    
FirstMatrix[0][0]=2    
FirstMatrix[0][1]=2    
FirstMatrix[1][0]=2    
FirstMatrix[1][1]=2    
FirstMatrix[0][0]=3    
FirstMatrix[0][1]=3    
FirstMatrix[1][0]=3    
FirstMatrix[1][1]=3    
FirstMatrix[0][0]=4    
FirstMatrix[0][1]=4    
FirstMatrix[1][0]=4    
FirstMatrix[1][1]=4       

How I want it to be:

FirstMatrix[0][0]=1   
FirstMatrix[0][1]=2    
FirstMatrix[1][0]=3    
FirstMatrix[1][1]=4           

Does anyone know how I can fix this print out please?

EDIT!! If I change code like this

    int[][] FirstMatrix = new int [k][l];

                while ((s = br.readLine()) != null) 
                {       
                    for(int i=0; i<k;i++)
                        for(int j=0; j<l; j++)
                        {
                        FirstMatrix[i][j] = Integer.parseInt(s);
                        }
                }
                br.close();

                for(int i=0; i<FirstMatrix.length; i++)
                    for(int j=0; j<FirstMatrix[i].length;j++)
                    {
                        System.out.println("FirstMatrix["+i+"]["+j+"]="+FirstMatrix[i][j]);
                    }

I get this output:

FirstMatrix[0][0]=4
FirstMatrix[0][1]=4
FirstMatrix[1][0]=4
FirstMatrix[1][1]=4
3
  • Notice how for each line, you are double-looping through your entire 2D array. You want to add the values in this order (0,0),(0,1),(1,0),(1,1) one at a time (i.e. you don't need loops). Try to discern the pattern. Hope this helps. Commented Dec 8, 2015 at 18:34
  • Yes, what you said here helps. Let me think for a solution. Thank you @ryuu9187 Commented Dec 8, 2015 at 18:37
  • without for loops I get FirstMatrix[0][0]=1 FirstMatrix[0][0]=2 FirstMatrix[0][0]=3 FirstMatrix[0][0]=4 Commented Dec 8, 2015 at 18:53

3 Answers 3

2

You're doing n*n times the loop. Because everytime that you read the file are entering the loop. If you know that every line is a number, you could read only one time and print each row.

Try it and let me know what happen.

Best regards from Mexico

Sign up to request clarification or add additional context in comments.

5 Comments

Greetings @Daniel Azamar ! It won't work ... :( but thank you for your suggestion
I'm reffering to "for" loop. You're doing j * i times. You don't need to do that. Try to remove the two "for" and see what happen!
I removed them but it don't work , without for loops I get FirstMatrix[0][0]=1 FirstMatrix[0][0]=2 FirstMatrix[0][0]=3 FirstMatrix[0][0]=4
Ok. I got it. And if you try with counters independiently from "for"? I mean: i++, so when i ==2, then make i = 0 according with your logic
I will try now and I'll let you know
1

After this line System.out.print("Elements in matrix are: \n"); paste this code.

int[][] FirstMatrix = new int [l][k];

        for(int j=0; j<l;j++)
        {  
            for(int i=0; i<k; i++) {
                s = br.readLine());
                FirstMatrix[j][i] = Integer.parseInt(s);
                System.out.println("FirstMatrix["+j+"]["+i+"]="+FirstMatrix[j][i]);
            }
        }

Don't use while loop.

1 Comment

Thank you for your reply, but now I get FirstMatrix[0][0]=1 FirstMatrix[1][0]=1 FirstMatrix[0][0]=2 FirstMatrix[1][0]=2 FirstMatrix[0][0]=3 FirstMatrix[1][0]=3 FirstMatrix[0][0]=4 FirstMatrix[1][0]=4
1
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ex_1 {
    public static void main(String args[]) throws IOException {
        FileReader fr = new FileReader("FirstMatrix.txt");
        BufferedReader br = new BufferedReader(fr);
        String s = br.readLine();
        String[] split = s.split("x");
        int k = Integer.parseInt(split[0]);
        int l = Integer.parseInt(split[1]);
        System.out.println("Matrix dimensions: " + k + " lines,  " + l + " columns si " + k * l + " elements");

        System.out.print("Elements in matrix are: \n");

        int[][] FirstMatrix = new int[k][l];

        for (int lineIndex = 0; lineIndex < k; lineIndex++) {


                for (int columnIndex = 0; columnIndex < l; columnIndex++) {
                    s = br.readLine();
                    FirstMatrix[lineIndex][columnIndex] = Integer.valueOf(s);
                }

        }

        for (int a = 0; a < k; a++) {
            for (int b = 0; b < l; b++)
                System.out.print(FirstMatrix[a][b] + " ");
            System.out.println();
        }

        br.close();

    }
}

2 Comments

Thank you for the code and for the time trying to build this code for me. Elements in my list are not like this 1 2 3 4 5, are 1 \n 2 \n 3 \n 4 If you know what I mean, elements are down in a row. I tryed to convert your code to my code, but it won't work... :(
Mulțumesc frumos :) acuma merge și varianta aceasta !

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.