Ok, so I'm having trouble figuring out how to assign numbers from a txt file to a String variable. My implementation ALMOST does what I want but its not quite right. With my code, it takes a line in the txt file and re-assigns it every time the loop restarts. Meaning by the time it outputs, the only thing shown is the very last line of the file. I would like it to contain the entirety of the txt file.
P.S.) I dont actually need to printout the variable, I only did it to verify whether or not it contains everything I need. I'm working on a much larger project and if I can get this working I should be able to adjust my actual project accordingly. Also, I would really appreciate it if any solution/help given doesn't try to make me use an array. If I have to turn the variable into an array then I would have to re-code everything else to fit it.
public class test {
public static String num;
public static void main(String[] args) {
BufferedReader reader = null;
try {
File file = new File("test3_14.txt");
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
num = line;
//System.out.println(num);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.print(num);
}
}