3

I got a small database in my text file. And it looks like this:

abc   def   zed   fgf

qwe   zxc   ghj   cvb ...

I want to convert it to:

Array1 = [abc,   def,   zed,   fgf]

Array2 = [qwe,   zxc,   ghj,   cvb] ...

And I will search words on it.

FileReader input = new FileReader("myFile");
BufferedReader bufRead = new BufferedReader(input);
String myLine = null;

while ( (myLine = bufRead.readLine()) != null)
{    
    String[] array1 = myLine.split(":");
    // check to make sure you have valid data
    String[] array2 = array1[1].split(" ");
    for (int i = 0; i < array2.length; i++)
        function(array1[0], array2[i]);
}

How can I do this with this sample code?

9
  • What is the problem with the code you have? Commented Apr 1, 2016 at 17:03
  • Text file is more than two lines.. Commented Apr 1, 2016 at 17:03
  • 1
    Did you mean for the entire line to be stored within the first index of Array? Or did you mean something like String[] Array1 where element 0 = abc, and element 1 = def, and element 2 = zed, etc.? Commented Apr 1, 2016 at 17:26
  • 1
    Why are you doing myLine.split(":"); doesn't that mean the file looks more like abc:def zed fgf. Commented Apr 1, 2016 at 17:39
  • 1
    @bubblebobble, I understand that. I'm asking about the length of the array. I think you meant Array1 = []{abc, def, zed, ...} and not Array1 = []{abc def zed ...}. These are completely different. In the first, you are creating an Array of length of values per line. In the second, you are creating an Array of length 1 with the entire line in the first index. Commented Apr 1, 2016 at 17:45

2 Answers 2

2

To have an array of arrays you could use an ArrayList like so:

    List<String[]> arrayList = new ArrayList<>();

    while ((myLine = bufRead.readLine()) != null) {
        String[] vals = myLine.split("   ");
        arrayList.add(vals);
    }

This will go through each line, make it into an array and then store it in the ArrayList.

After, you can iterate through your ArrayList like this:

for (String[] currLine : arrayList) {
    for (String currString : currLine) {
         System.out.print(currString + " ");
    }
    System.out.println();
}

This will print:

run:
abc def zed fgf
qwe zxc ghj cvb
BUILD SUCCESSFUL (total time: 0 seconds)

EDIT Created a method that will find the index of the value you're looking for. However I suggest that searching for something like "zxc" result in 1,1 and not 2,2 because arrays are indexed at 0.

public static int[] getIndex(List<String[]> arrayList, String tofind) {
    int[] index = new int[]{-1, -1};
    for (int i = 0; i < arrayList.size(); i++) {
        String[] currLine = arrayList.get(i);
        for (int j = 0; j < currLine.length; j++) {
            if (currLine[j].equals(tofind)) {
                index = new int[]{i + 1, j + 1};
                return index;
            }
        }
    }
    return index;
}

While not the most efficient way (it iterates through every Array and every String of that Array) it does get you the result that you're looking for:

Calling it like so:

    int[] zxcIndex = getIndex(arrayList, "zxc");
    System.out.println(zxcIndex[0] + ", " + zxcIndex[1]);

Will print:

2, 2

I wrote this print method up while working on this, you can use it to your convenience for debugging:

public static void printList(List<String[]> arrayList) {
    for (String[] currLine : arrayList) {
        for (String currString : currLine) {
            System.out.print(currString + " ");
        }
        System.out.println();
    }
}

Also, figured you might want to update at a given index, this is a lot easier:

public static void updateIndex(List<String[]> arrayList, int[] toUpdate, String value) {
    String[] rowToUpdate = arrayList.get(toUpdate[0] - 1);
    rowToUpdate[toUpdate[1] - 1] = value;
}

So putting this all together, running the following:

    System.out.println("Current list:");
    printList(arrayList);

    int[] zxcIndex = getIndex(arrayList, "zxc");
    System.out.println("\nIndex of xzc is: " + zxcIndex[0] + ", " + zxcIndex[1] + "\n");

    updateIndex(arrayList, zxcIndex, "lmnop");

    System.out.println("Modified list at index " + zxcIndex[0] + "," + zxcIndex[1] + " :");
    printList(arrayList);

results in:

run:
Current list:
abc def zed fgf
qwe zxc ghj cvb

Index of xzc is: 2, 2

Modified list at index 2,2 :
abc def zed fgf
qwe lmnop ghj cvb
BUILD SUCCESSFUL (total time: 0 seconds)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. How can I search any string and could say that its index?
Give me an example of something you're searching for.
zxc and I want to know that it is like at List[x][y]. So when I print that indexes I could easily reach that value :/
What do you expect the result of searching for zxc be? The Array index that contains xzc? So in this case qwe zxc ghj cvb which would return 1 (since it's the second array in the ArrayList)?
@bubblebobble, okay, let me come up with something.
|
0

String[][] will help you. For each line, you will have an array which contains two other arrays with values split by your delimiter.

String[][] array = new String[(int)bufRead.lines().count()][2];
for(int i = 0; (myLine = bufRead.readLine()) != null); ++i) {    
   array[i][0] = myLine.split(":");
   array[i][1] = array[i][0].split(" ");
   ...
}

5 Comments

Is it only for two lines of the text or more than two lines?
that's for all lines of the file
How can we find the [SIZE]?
@bubblebobble, SIZE == (int)bufRead.lines().count(), I corrected
@bubblebobble, the solution for java 8

Your Answer

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