JAVA: This program reads in from a file called File.txt. Each line of the file i
ID: 3875506 • Letter: J
Question
JAVA: This program reads in from a file called File.txt. Each line of the file is then stored in an array. But then that array needs to be stored in separate arrays by year. How is this achieved? I thought I had it but nothing shows up in my 2014 and 2015 arrays.
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[][] array2014 = new String[10][6];
String[][] array2015 = new String[10][6];
String[] array1 = new String[6];
Driver obj = new Driver();
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int i = 0, k = 0;
while (inFile.hasNextLine()) {
oneLine = inFile.nextLine();
array1 = oneLine.split("[, ]");
if(array1[0] == "2014") {
for(int j = 0; j < array1.length; j++)
array2014[i][j] = array1[j];
i++;
}
else if(array1[0] == "2015") {
for(int j = 0; j < array1.length; j++) {
array2015[k][j] = array1[j];
k++;
}
}
}
System.out.println(Arrays.deepToString(array2014));
System.out.println(Arrays.deepToString(array2015));
}
}
File.txt
2015 Employee Campbell,Steve 3000
2014 Salesman Sanchez,Carlos 4000 200000
2015 Executive Oduala,Barack 600
Explanation / Answer
There was just a minor issue. You will need to use equals() methods on strings instead of == for comparison. I have used equals method and also the the variable k representing the length of the array for 2015 was incremented inside for loop. So separated that out. Also renamed i,k to len2014 and len2015. Given below is the fixed code. Hope it helps.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
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[][] array2014 = new String[10][6];
String[][] array2015 = new String[10][6];
String[] array1 = new String[6];
Driver obj = new Driver();
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);
String oneLine;
int len2014 = 0, len2015 = 0;
while (inFile.hasNextLine()) {
> array1 = oneLine.split("[, ]");
if (array1[0].equals("2014")) {
for (int j = 0; j < array1.length; j++)
array2014[len2014][j] = array1[j];
len2014++;
}
else if (array1[0].equals("2015")) {
for (int j = 0; j < array1.length; j++)
array2015[len2015][j] = array1[j];
len2015++;
}
}
System.out.println(Arrays.deepToString(array2014));
System.out.println(Arrays.deepToString(array2015));
}
}
output
=======
[[2014, Salesman, Sanchez, Carlos, 4000, 200000], [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], [null, null, null, null, null, null], [null, null, null, null, null, null]]
[[2015, Employee, Campbell, Steve, 3000, null], [2015, Executive, Oduala, Barack, 600, 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, null], [null, null, null, null, null, null]]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.