Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: read int from file into 3d array - FileReader? Help pls

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default read int from file into 3d array - FileReader? Help pls

    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
    Last edited by AleXusher; May 10th, 2021 at 04:20 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: read int from file into 3d array - FileReader? Help pls

    But i fail to do so
    Please explain what happens when the program is executed? What does "fail" mean?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: read int from file into 3d array - FileReader? Help pls

    If i execute i get

    Description Resource Path Location Type
    The method read(CharBuffer) in the type Reader is not applicable for the arguments (int) lesen.java /3darraylesen/src line 20 Java Problem

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: read int from file into 3d array - FileReader? Help pls

    Ok, that is an error from the compiler saying the there is no read method in the Reader class that takes an int as an argument.

    The file that is it trying to read contains text created by the println method. The text "123" is a String and is not the same as the int value 123.
    An easy way to read text from a file and convert it to an int value is with the Scanner class's nextInt method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to read file into n-bit array
    By hkocak in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 20th, 2013, 01:55 PM
  2. Where did my char(s) go?! FileReader read(CharBuffer target) throwing file contents away...?
    By TooOldForThisStuff in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 22nd, 2013, 02:38 PM
  3. [SOLVED] Why is my array not being read in correctly from my file?
    By gabie1121 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2013, 10:55 AM
  4. How do we fill in an array using substring, FileReader and a txt file?
    By wholegrain in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2012, 03:18 PM
  5. [SOLVED] FileReader.read() reads garbage
    By Hackworth in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 27th, 2010, 01:06 PM

Tags for this Thread