Java – instantiate ArrayList by reading rows from scanner, where to declare objects?
I want to fill the array list with lines from the input file, which is as follows:
7f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000027f00000000000000000000000000000000000000000000000000000000000000020101 7f00000000000000000000000000000000000000000000000000000000000000037f00000000000000000000000000000000000000000000000000000000000000037f00000000000000000000000000000000000000000000000000000000000000030101 7f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000047f00000000000000000000000000000000000000000000000000000000000000040101 7f00000000000000000000000000000000000000000000000000000000000000057f00000000000000000000000000000000000000000000000000000000000000057f00000000000000000000000000000000000000000000000000000000000000050101 7f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000067f00000000000000000000000000000000000000000000000000000000000000060101
I want the data object in Java created based on this to take each of these rows as a new string, and they will exist together in the list, so to speak *
Therefore, when I try to read file lines into different components of this array list, I can't figure out where I need to declare the array list in the main program My plan is to populate it in a separate way:
import java.io.*;
import java.util.Scanner;
import java.util.List;
import java.util.Array;
import java.util.ArrayList;
class evmTest {
public static void main(String[] args) {
Array<String> inputLinesObject = new ArrayList<String>();
// populate from file
inputLinesObject = readFile("/Users/s.matthew.english/codes.txt",inputLinesObject);
System.out.println(Array.toString(inputLinesObject));
}
private static void readFile(String fileName,Array<String> inputLines) {
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
// System.out.println(scanner.nextLine());
inputLines.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return inputLines;
}
}
Maybe I can initially instantiate it as null and pass the empty array list to the method to be populated?
*The terminology in the last sentence is not entirely accurate - please forgive me - I'm readjusting my java vocabulary, but I think it should be clear enough what I'm trying to do If not, please let me know and I will be happy to clarify
Solution
Use this code for the readfile() method:
private static List<String> readFile(String fileName,List<String> inputLines) {
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
// System.out.println(scanner.nextLine());
inputLines.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return inputLines;
}
Then call this readFile () method as follows:
List<String> inputLinesObject = new ArrayList<String>();
inputLinesObject = readFile("/Users/s.matthew.english/codes.txt",inputLinesObject);
for(String str : inputLinesObject){
System.out.println(str);
}
