Sharlabean Sharlabean - 1 year ago 77
Java Question

Java: How can I selectively scan a file into two 2d string arrays

How can I selectively scan a file into two 2d string arrays?

I have a file that looks like this:

2014 fruit apple red 50 30
2015 vegetable spinach green 10
2014 fruit orange orange 100
2014 fruit banana yellow 90 100
2015 vegetable corn yellow 120

I'm trying to read the file and put each line into a 2d string array (delimited by spaces). I want an array that is formed from just the lines that begin with 2014, and a second array that is formed from lines that begin with 2015.

Because it is for a class, I'm stuck with the strict parameters that 1. it has to be from a text file, and 2. it has to go into two 2d arrays.

I'm able to successfully read the whole file into one 2d String array, but everything I try for two arrays seems to fail. Here's what I've got...
Successful single 2d array:

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

//Initializes the variables needed to read the line length of the file
FileReader fr = null;
LineNumberReader lnr = null;
File newFile = new File("ProjectTest.txt");

//Initiallizes the variables needed to create the 2D array
BufferedReader inputStream = new BufferedReader(new FileReader(newFile));
Scanner scannerIn = new Scanner(inputStream);

//Other variables
int lineNum = 0;
int linesCounter = 0;
String str;

//Determines how many lines in the file
try {
fr = new FileReader(newFile);
lnr = new LineNumberReader(fr);
while (lnr.readLine() != null) {
} catch (Exception e) {
} finally {
if (fr != null) {
if (lnr != null) {

//Create the 2D array
String arrayA[][] = new String[lineNum][];

while (scannerIn.hasNextLine() && linesCounter < lineNum) {
arrayA[linesCounter] = scannerIn.nextLine().split("\\s");

//Test-print the array
for (int i = 0; i< lineNum; i++){

I've tried many different ways to try and get this data into two arrays, but I think my confusion comes from knowing when and how to use scanners vs bufferedreaders. I've tried if/then statements, reading just the 2014 lines into an array, and i've tried copying one array to another array. Needless to say, I've gotten them all wrong. Here's one of many attempts at two 2d arrays using if/then statements:

String arrayA[][] = new String[lineNum][];
String arrayB[][] = new String[lineNum][];

String line = inputStream.readLine();

while (line != null) {
if (line.contains("2014")){
arrayA[linesCounter] = inputStream.readLine().split("\\s");
} else {
arrayB[linesCounter] = inputStream.readLine().split("\\s");

Please help me!! I appreciate you all!

Answer Source

Here's one way to do it:

String[][] arrayA = new String[lineNum][];
String[][[ arrayB = new String[lineNum][];

int counterA = 0;
int counterB = 0;

while (scannerIn.hasNextLine()) {
    String line = scannerIn.nextLine();
    String[] parts = line.split("\\s");
    if (line.startsWith("2014")) {
        arrayA[counterA++] = parts;
    } else if (line.startsWith("2015")) {
        arrayB[counterB++] = parts;

// prune excess elements
arrayA = Arrays.copyOf(arrayA, counterA);
arrayB = Arrays.copyOf(arrayB, counterB);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download