Hello there.
I have an assignmet where i have too make it so that i can read a txt file and turn it into an array and then print out the numbers.
I have numbers between 1 and 100, all random numbers, within a txt file.
The problem that i have atm is that i cannot print out anything from my arraylist.
I am doing something wrong but cannot tell what exactly, any ideas?
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public interface Histrogram { // throws Exception is there to check if the file actually exists and if it // doesn't then it will tell us that by an error. public static void main(String[] args) throws Exception { // read(); } static void read() throws Exception { ArrayList<Integer> list = new ArrayList<Integer>(); // This method just reads a file. // The path that we want to read from. FileReader file = new FileReader("C:\\Users\\mrLowBot\\Desktop\\heltal.txt"); // Reading it by BufferedReader. BufferedReader read = new BufferedReader(file); int[] data = new int[100]; String text = ""; int data1 = 0; int i = 0; // Actually reading it here. String line = read.readLine(); while (line != null) { text += line; // line = read.readLine(); data1 = Integer.valueOf(read.readLine()); list.add(data1); } // System.out.println(text); System.out.println(); for (int k : list) { System.out.println(k); } } }
PS: Some code is under the "//" lines but that's only because i have tried to only print the numbers as they are and without trying to add them into an array and that worked fine.