سؤال

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]);
        }

    }
}
هل كانت مفيدة؟

المحلول

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"
}
}

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top