I'm pretty new to Java and I'm having difficulty printing the array created from reading a .txt file. I cannot figure out what is i've done wrong. Also, how would I put the .txt file into the same directory as my class? these have to be seperate methods,
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayOperations {
// The code for the method readFromFile is given below.
public static void main(String[] args){
}
public static ArrayList<Integer> readFromFile(String fileName)
throws FileNotFoundException
{
File f = new File("data.txt");
Scanner fileIn = new Scanner (f);
ArrayList<Integer> list = new ArrayList<Integer>();
while (fileIn.hasNextInt()){ // quit when you encounter ‘Q’
int num = fileIn.nextInt();
list.add(num);
}// end while
fileIn.close();
return list;
} // end readFromFile
public static String printArray(int[] readFromFile){
String str = "";
for(int i = 0; i < 10; i++){
str = str + Integer.toString(readFromFile[i]) + " ";
}
return str;
}
}
This is the .txt file named Data.txt
the output i'm trying to get is this:
The data in the file is:
45 32 97 87 64 37 65 72 84 22 58 65 72 89 93 95