简体   繁体   中英

Why can't I access my 2D array outside of the loop?

I'm having some trouble accessing my 2D array (myArray) outside of this the loop. I want to access it using other methods, but I can't even access it in this method. It prints out correctly as it's looping, but the test print of

System.out.println(myArray[10][2]);

is always null. So it's like the array isn't actually filling or something. Anyone know what I'm doing wrong here?

package titanic;

import java.io.*;
import java.util.*;

public class Titanic {


public static final int ROW = 1309;
public static final int COLUMN = 6;
public static String [][] myArray = new String[ROW][COLUMN];


public static String[][] arraySetup(){

    int recordCounter = 0;
    String[][] myArray = new String[ROW][COLUMN];
    String[] name = new String [ROW];
    try {
        BufferedReader br = new BufferedReader(new FileReader("C:/Users/Tom/Desktop/Titanic.txt"));
        String line;
        for (int i = 0; i < 1309; i++){
        while((line = br.readLine()) != null){

            String tmp[] = line.split("\t");
                myArray[i][0] = tmp[0];
                myArray[i][1] = tmp[1];
                myArray[i][2] = tmp[2];
                myArray[i][3] = tmp[3];
                myArray[i][4] = tmp[4];
                myArray[i][5] = tmp[5];
                System.out.println("myArray[i][5] = " + myArray[i][5]);
                recordCounter++;

            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(myArray[10][2]);

    System.out.println(recordCounter + " records.");
    return myArray;
}

As you have you while loop inside for loop that is used to for indexing your output array while loop always writes into the myArray[0][0] to myArray[0][5]

    for (int i = 0; i < 1309; i++){   // i is 0
    while((line = br.readLine()) != null){   // you go through all the lines while i is 0

        String tmp[] = line.split("\t");
            myArray[i][0] = tmp[0];
            myArray[i][1] = tmp[1];
            myArray[i][2] = tmp[2];
            myArray[i][3] = tmp[3];
            myArray[i][4] = tmp[4];
            myArray[i][5] = tmp[5];
            System.out.println("myArray[i][5] = " + myArray[i][5]);
            recordCounter++;

        }
    }

Because of that your check always returns null.

System.out.println(myArray[10][2]);

Don't use for and while loop together. During the first for loop, your while reads all file contents and all other array positions remain empty. Try this instead:

int i=0;
while ((line = br.readLine()) != null){
    String tmp[] = line.split("\t");
     myArray[i][0] = tmp[0];
     myArray[i][1] = tmp[1];
     ...       
     myArray[i][5] = tmp[5];

     i++;
     System.out.println("myArray[i][5] = " + myArray[i][5]);
     recordCounter++;
}

I think the problem in your code is right there

for (int i = 0; i < 1309; i++){ // you loop 1308 time
        while((line = br.readLine()) != null){ /* in each loop, you loop 
 until you have read all the file so you read 1308 time the file. But when 
you reach the end of file on the first iterration, it wont read the file on the 1307
 other iterration  and all data will be store in myArray[0][0-5]*/

            String tmp[] = line.split("\t");
                myArray[i][0] = tmp[0];
                myArray[i][1] = tmp[1];
                myArray[i][2] = tmp[2];
                myArray[i][3] = tmp[3];
                myArray[i][4] = tmp[4];
                myArray[i][5] = tmp[5];
                System.out.println("myArray[i][5] = " + myArray[i][5]);
                recordCounter++;

            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM