-2

Very beginner to java. My program prints just a long column instead of a 2d array. For testing I use a file with 10 lines and 20 columns, but the actual file will be 20 columns and a few thousands lines, I don't know how many. I've read all the posts I could find on the internet on this topic but still couldn't get the program to work. Any ideas?

 1 2 4 6 9 13 15 16 21 28 34 37 41 48 50 52 53 54 57 68
 6 7 10 17 23 24 27 28 31 39 42 43 46 48 50 55 60 61 67 70
 2 3 5 7 11 14 15 20 28 45 46 47 48 52 56 61 62 63 66 70
 4 5 7 11 13 15 19 23 24 27 28 35 38 40 48 50 57 58 64 66
 3 8 20 26 27 32 36 38 39 43 45 47 50 53 54 56 59 61 67 68
 1 3 5 7 15 19 26 30 31 36 41 44 48 49 56 58 59 60 61 65
 1 2 4 6 9 13 15 16 21 28 34 37 41 48 50 52 53 54 57 68
 6 7 10 17 23 24 27 28 31 39 42 43 46 48 50 55 60 61 67 70
 2 3 5 7 11 14 15 20 28 45 46 47 48 52 56 61 62 63 66 70
 4 5 7 11 13 15 19 23 24 27 28 35 38 40 48 50 57 58 64 66

Here is the code

try {
    FileInputStream fstream = new FileInputStream("C:\\keno.txt");

    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine = "";
    String[] tokens;

    //Read File Line By Line
    int row = 0;
    String userdata [][]= new String [10][21];
    while ((strLine = br.readLine()) != null)   {
        // Copy the content into the array
        tokens = strLine.split(" +");
        for(int j = 0; j < tokens.length; j++) {
            userdata[row][j] = tokens[j];
        }
        row++;
    }
    for(int i = 0; i < userdata.length; i++) {
        for(int j=0; j < userdata[i].length; j++){
            System.out.println(userdata[i][j]);
        }  
    }   
    in.close();
} catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
greuceanu
  • 13
  • 6
  • `"Any ideas?"` -- break the big problem into its smaller steps, including reading in text files, parsing a line of text, creating arrays, etc. and then come back with a much more specific and answerable question if stuck in one of these steps. `"I've read all the posts I could find on the internet on this topic but still couldn't get the program to work."` -- and you've not shown us what is not working for you, and without this, how can wel help? Please show us your pertinent code and any descriptions of misbehaviors. Voting to close pending your posting of this important information. – Hovercraft Full Of Eels Apr 30 '16 at 16:03
  • [You've already asked this question on this site](http://stackoverflow.com/questions/36656388/processing-range-of-lines-in-a-2d-array-or-file)! – Hovercraft Full Of Eels Apr 30 '16 at 16:05
  • Trying to post the code, the site just doesn't allow me. – greuceanu Apr 30 '16 at 16:30
  • Read the messages that the site is giving back to you -- you need to post more than just code, you need to also post substantial information about what the code is supposed to be doing as text. Also don't post code in a link or post too much code. You need to create and post a [mcve], and this usually means creating a small separate program for this purpose. – Hovercraft Full Of Eels Apr 30 '16 at 16:32
  • @hovercraft-full-of-eels: I would have posted my code 40 minutes ago if this site was any user friendly at all. – greuceanu Apr 30 '16 at 16:50
  • You'll find much greater friendliness here and on any question/answer site if you read the rules of the site **before** posting. Please start with the [tour] and then the [help] section, especially the section on how to ask questions. – Hovercraft Full Of Eels Apr 30 '16 at 16:55

1 Answers1

0


i wrote a short programm that does what i think you want to do. my code is first extracting all the data from the txt-file to a stringarray, then split each string by the seperator " " (space). At the end the string is being parsed to long format. hope this helps you.
IMPORTANT NOTE: the txt-file must have equal dimensions, meaning if the first row has 20 elements, EACH ROW must have 20 elements. AND: each row must end with an element, not with a space. Let me know if this was helpfull and if not, where you had trouble!
greetz

import java.io.*;
import java.util.ArrayList;

public class ReadFileInto2dArray{

    public static void main(String[] arg) throws FileNotFoundException, IOException, NumberFormatException {

        // IMPORT DATA FROM TXT FILE AS STRINGARRAY
        String filedirectory = "C:\\Users\\thomas\\Desktop\\Neuer Ordner\\keno.txt";
        BufferedReader b = new BufferedReader(new FileReader(filedirectory));
        String str=null;
        ArrayList<String> lines = new ArrayList<String>();
        while((str = b.readLine()) != null){
            lines.add(str);
        }
        String[] strArr = lines.toArray(new String[lines.size()]);  
        b.close();

        // GET DIMENSIONS: number of rows       
        int nRows = strArr.length;                                                                          
        // GET DIMENSIONS: number of elements in the first line
        int nCols = (strArr[0].length()-strArr[0].replace(" ", "").length())+1;     

        // INITIALIZE LONG 2D ARRAY (MATRIX)
        long[][] data = new long[nRows][nCols];

        // SPLIT EACH STRING OF ROW INTO SUBSTRING AND PARSE TO LONG FORMAT
        String[] split = new String[nCols];
        for (int r=0; r<nRows; r++){
            split = strArr[r].split(" ");
            for (int c=0; c<nCols; c++) {
                data[r][c] = Long.parseLong(split[c]);
            }
        }
        b.close();

        // SHOW THAT IT WORKED
        System.out.println("first element: data[0][0] should be 1: " + data[0][0]);
        System.out.println("last element data[9][19] should be 66: " + data[9][19]);

    }
}
dcts
  • 1,479
  • 15
  • 34
  • it gives me an error "Exception in thread "main" java.lang.NumberFormatException: For input string: "" – greuceanu Apr 30 '16 at 20:50
  • and a few others: at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631).Do you use Netbeans? – greuceanu Apr 30 '16 at 20:55
  • i used eclipse! but i found the mistake, i forgot to throw one exception, now its fixed (i updated the code)! it works when you dont use any IDE at all and just compile it in the cmd window. IMPORTANT: if you use mac, then the "filedirectory" string must seperate folders with "/" instead of "\\". hope it works now, let me know. – dcts Apr 30 '16 at 23:28
  • I ran the program on command prompt, it gives me the same errors as Netbeans – greuceanu May 03 '16 at 00:10
  • Anyone wants to take a stab at this problem? – greuceanu May 05 '16 at 23:38
  • Hmm this is strange... I also get errors but only when the file is not stored properly. When the keno.txt file is at the right place its fine for my maschine... Can you send me your email, then i can send you a small video, maybe that helps :/ – dcts May 07 '16 at 03:37
  • my email is greuceanu1231@gmail.com – greuceanu May 10 '16 at 23:20
  • no more ideas? C'mon guys , this is a simple problem – greuceanu May 13 '16 at 16:47