Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: I cannot get all the lines of the file, File.txt, stored into my 2D array.

ID: 3875459 • Letter: J

Question

JAVA: I cannot get all the lines of the file, File.txt, stored into my 2D array. Only the last line of the file is stored into my 2D array when I print the array at the end of the program.

Driver.java

package project1;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Scanner;

public class Driver {

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

String[][] array1 = new String[10][6];

String[] array11 = new String[6];

Driver obj = new Driver();

File myFile = new File(args[0]);

Scanner inFile = new Scanner(myFile);

String oneLine;

int i = 0, j = 0, k = 0 ;

while (inFile.hasNextLine()) {

oneLine = inFile.nextLine();

array11 = oneLine.split("[, ]");

obj.getArray(i, k, array11, array2015);

}

//return Arrays.deepToString(array1);

System.out.println(Arrays.deepToString(obj.getArray(i, k, array11, array1)));

}

public String[][] getArray(int i, int k, String[] array11, String[][] array1) {

for(k = 0; k < array11.length; k++)

array1[i][k] = array11[k];

i++;

return array1;

}

}

File.txt

2015 Employee Campbell,Steve 3000
2014 Salesman Sanchez,Carlos 4000 200000
2015 Executive Oduala,Barack 6000 60

Explanation / Answer

Explanation:

Please find the below updated driver class and coresponding sample output.

Issue was missing counter in while loop. Counter value i should be increase in while loop insated of getArray method. Also to print 2D array no need to call getArray method again inside deepToString method. Just need to pass 2D array to print the elements.

Please revert back in case required more information or need to change anything.

Updated Program:


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
String[][] array1 = new String[10][6];
String[] array11 = new String[6];
Driver obj = new Driver();
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int i = 0, j = 0, k = 0;
while (inFile.hasNextLine()) {
> array11 = oneLine.split("[, ]");
obj.getArray(i, k, array11, array1);
i++;
}
// return Arrays.deepToString(array1);
System.out.println(Arrays.deepToString(array1));
}
public String[][] getArray(int i, int k, String[] array11, String[][] array1) {
for (k = 0; k < array11.length; k++) {
array1[i][k] = array11[k];
}
return array1;
}
}

Output:


[[2015, Employee, Campbell, Steve, 3000, null], [2014, Salesman, Sanchez, Carlos, 4000, 200000], [2015, Executive, Oduala, Barack, 6000, 60], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null]]