0

I am trying to create a 2d array of size [3] and [100] the idea being it will store three strings which will be stored in a text file separated by tab. I have scoured the web for any help with this but failed to find any good answer. It needs to be an Array that it is stored in through Java.

It should be something like this:

  • associates the String File with filename.txt
  • create a 2d Array size 3 colums and 100 rows. here it should store the data from left to right with 3 strings on every line then seperated onto new lines.
  • then I need a way to add the string in the text file to the array. and at the end it should println the contents of the array.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class ArrayDirectory {
    public static void main(String args[]) {
        String file = ("directory.txt");
        // initialises the file linked to the String file
        String[][] Entry = new String[3][50];
        // creates a 2d array with 3 columns and 50 rows.
        readFromFile.openTextFile(file);
        //should read from file
        String line = readFromFile.readLine();

        int count = 0;
        while (line != null) {
            input[count] = String.split("");
            Entry = readFromFile.readline();

        }

        for (int h = 0; h < input.length; h++) {
            System.out.println(input[h]);
        }

    }
}
5
  • 2
    show your code anyway, we won't mock :) Commented May 1, 2014 at 10:12
  • Post your code anyway. People won't mock you for it, they'll only help to show you where you might be going wrong. It's also very difficult to help without being able to see the code. Commented May 1, 2014 at 10:12
  • have posted what Ive got now. Im terrible at Java only ever really worked with JS and CSS etc. So it is probably wrong in multiple places Commented May 1, 2014 at 10:17
  • where are readFromFile and input initialised? Commented May 1, 2014 at 10:25
  • I haven't done that. this was taken from a number of different example and I have tried to put it together really quick, I've gone through so many different pieces of code and this is currently what I have... My apologies. Commented May 1, 2014 at 10:29

2 Answers 2

1

A few points:

  • you didnt have any way of reading the file.
  • your array had 3 rows and 50 columns when your comment stated the opposite
  • you were trying to split on a space, not a tab.

see comments in code explaining what it does:

public class ArrayDirectory {
public static void main(String args[]) throws FileNotFoundException {
    String file = ("lab4b2.txt");
    Scanner scan = new Scanner(new FileReader(file));
    // initialises the scanner to read the file file

    String[][] entries = new String[100][3];
    // creates a 2d array with 100 rows and 3 columns.

    int i = 0;
    while(scan.hasNextLine()){
        entries[i] = scan.nextLine().split("\t");
        i++;
    }
    //loops through the file and splits on a tab

    for (int row = 0; row < entries.length; row++) {
        for (int col = 0; col < entries[0].length; col++) {
            if(entries[row][col] != null){
                System.out.print(entries[row][col] + " " );
            }
        }
        if(entries[row][0] != null){
            System.out.print("\n");
        }
    }
    //prints the contents of the array that are not "null"
}
}
Sign up to request clarification or add additional context in comments.

9 Comments

will the file always have 100 rows?
Edit: this only prints positions 0,1,0,2 and 0,3. any ideas why?
the amount of rows in the file is currently only 4 but if another record is added then the array will have to increase in size. So I set it at 100 as I think that would be enough rows
it only prints the first record in the text file :(
it prints all for me, show an example of the file contents in your question
|
0
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class ArrayDirectory{

        public static void main(String args[]) throws IOException {
            BufferedReader readFromFile = new BufferedReader(new FileReader("test.txt"));


            int count = 0;
            String[][] entry = new String[100][3];

            String line = readFromFile.readLine();



            while (line != null) {
                //I don't know the layout of your file, but i guess it is: "XX YY ZZ", with spaces inbetween.
                String[] temp = line.split(" "); //temp here will be: [XX][YY][ZZ]
                for (int i = 0; i < temp.length; i++) {
                    entry[count][i] = temp[i];
                }
                line = readFromFile.readLine();
                count++;


            }
            readFromFile.close();
            for (int j = 0; j < entry.length; j++) {
                for (int k = 0; k < entry[0].length; k++) {

                    System.out.print(entry[j][k] + " ");
                }
                System.out.println();
            }

        }
}

Read about string split here: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)

Also, your variables should always start with small letters, only classes should have big first letter.

6 Comments

thanks for that I thought it was going to work perfectly for mine. but im getting the following error Exception in thread "main" java.lang.NullPointerException at ArrayDirectory.main(ArrayDirectory.java:18)
Try again, I didn't get any exceptions from my code. Make sure it's the absolute path to the file, if it isn't in your workspace folder.
it only reads the first 3 strings in the file now. that something that @Sionnach733 seems to have gotten working but I think im understanding better how 2d arrays work and need it to store print it out like position 00,01,02 then new line then 10,11,12 and so on till it reaches the end of the file
Works now, just a typo I made.
That is perfect. Thanks. btw do you have any idea why my eclipse puts a box between each letter from the text file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.