Reputation: 13
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) {
lineNum++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (fr != null) {
fr.close();
}
if (lnr != null) {
lnr.close();
}
}
//Create the 2D array
String arrayA[][] = new String[lineNum][];
while (scannerIn.hasNextLine() && linesCounter < lineNum) {
arrayA[linesCounter] = scannerIn.nextLine().split("\\s");
linesCounter++;
}
//Test-print the array
for (int i = 0; i< lineNum; i++){
System.out.println(arrayA[i][2]);
}
}
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");
linesCounter++;
} else {
arrayB[linesCounter] = inputStream.readLine().split("\\s");
linesCounter++;
}
}
Please help me!! I appreciate you all!
Upvotes: 1
Views: 105
Reputation: 124804
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);
Upvotes: 1
Reputation: 1681
In most cases, it's better to collect data in a collection, not an array.
I would just create two array lists, e.g. of type
ArrayList<ArrayList<String>>
and fill them line by line. No need to count anything in advance.
And then simply create arrays from the lists, which is easy now that the sizes are known :)
Example with a single list:
List<List<String>> list = new ArrayList<>();
// fill the list of lists...
// convert the list of lists to a 2D array...
String[][] array = new String[list.size()][];
int index = 0;
for (List<String> sublist : list)
{
array[index] = sublist.toArray(new String[sublist.size()]);
++index;
}
Upvotes: 0
Reputation: 1825
One way of doing that would be :
File fin = new File("your_file_path");
FileInputStream fis = new FileInputStream(fin);
//Construct BufferedReader from InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int lineNum = 1024;
String [][] linesWith2014 = new String[lineNum][];
String [][] linesWith2015 = new String[lineNum][];
int countWith2014 = 0 ;
int countWith2015 = 0;
while ((line = br.readLine()) != null) {
System.out.println(line);
if (line.startsWith("2014")){
linesWith2014[countWith2014++] = line.split("\\s+");
}else if (line.startsWith("2015")){
linesWith2015[countWith2015++] = line.split("\\s+");
}
}
br.close();
linesWith2014 = Arrays.copyOf(linesWith2014, countWith2014);
linesWith2015 = Arrays.copyOf(linesWith2015, countWith2015);
And of course surround it with try catch.
Upvotes: 0
Reputation: 541
Try splitting the string before you run the if else statement. Then you have a target of
tempString[0]
To compare to the date
Upvotes: 0