Hello there,
i got an txt file with int values. Each line has exactly one number. Now i want to read those numbers into an 3d array. But i fail to do so. Can someone help me?
i copied test.txt to the project and renamed it, so it fits the code.
This is the code i have so far:
import java.io.BufferedReader; import java.io.FileReader; import java.util.Arrays; import java.util.Scanner; public class lesen { public static void main (String args[]) throws Exception { //Scanner sc = new Scanner(new BufferedReader(new FileReader("sample.txt"))); FileReader inputFile = new FileReader("Datenzumlesen.txt"); int boxes = 10; int rows = 10; int columns = 10; int [][][] myArray = new int[boxes][rows][columns]; for(int i=0;i<10;i++) { for (int j=0;j<10;j++) { for(int k=0;k<10;k++) { inputFile.read(myArray[i][j][k]); //(myArray[i][j][k]); } } } inputFile.close(); for (int i=0; i < 10; i++) { for (int j=0; j < 10; j++) { for (int k=0; k < 10; k++) { //System.out.println("Test"); System.out.print(myArray[i][j][k]+" "); } System.out.println(""); } System.out.println("i ist "+i); } } }
For creation of the values and saving i used the following code:
import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import java.io.File; public class speichern{ public static void main (String args[]) throws FileNotFoundException { PrintWriter outputFile = new PrintWriter("test.txt"); int [][][]array = new int[10][10][10]; int []array2 = new int[10]; int i = 0; int j = 0; int k = 0; int min = 50; int max = 100; //Generate random int value from 50 to 100 System.out.println("Random value in int from "+min+" to "+max+ ":"); for (i=0; i < 10; i++) { for (j=0; j < 10; j++) { for (k=0; k < 10; k++) { //System.out.println("Test"); array[i][j][k] = (int)Math.floor(Math.random()*(max-min+1)+min); outputFile.println(array[i][j][k]); } } //array2[i] = (int)Math.floor(Math.random()*(max-min+1)+min); //outputFile.println(array2[i]); } outputFile.close(); for (i=0; i < 10; i++) { for (j=0; j < 10; j++) { for (k=0; k < 10; k++) { //System.out.println("Test"); System.out.print(array[i][j][k]+" "); } System.out.println(""); } System.out.println("i ist "+i); } System.out.println(array[8][1][3]); } }
Hope someone can help me out