0

Hi,
I have been spending several weeks figuring this out but couldn`t find way to it. Here is what I am planning to do, I have a text file with several data in it, and I have to extract part of the data into a 2 dimensional array.

Here is a sample of the text file where each line represent data in each row in text file:
A
B
C
D
A>3;C>2
D>5;B<3

I want to make a 2 dimensional string array and put it in this form
A B C D
>3 <3 >2 >5 <-- so this sample is a 2x4 array

Here is how I got it: First read through the first 4 row then put each of them in a column. Then read through other 2 row, check which character matches whichof the first 4 rows and put the comparison sign and number under it in the 2 dimensional array. So for example when I get to line"A>3;B>2" I check which column A is in and put >3 under it, when encounter a ";" move to check next one on the line, so check which column C is in and put >2 under it in the new two dimensional array... and so on. I don`t know how to write a java code to extract like what I wanted.

What I have done so far is I was able to get each row into a 1 dimensional string array each from reading the textfile in,
so now I got a string array to have such content:
text[0]=A
text[1]=B
.
.
text[5]=C>5;D<3

But I want to turn those text[] string array into the 2 dimensional string array form that I want. How do I extract content of each string array? How should I start?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by VernonDozier
0

Hi,
I have been spending several weeks figuring this out but couldn`t find way to it. Here is what I am planning to do, I have a text file with several data in it, and I have to extract part of the data into a 2 dimensional array.

Here is a sample of the text file where each line represent data in each row in text file:
A
B
C
D
A>3;C>2
D>5;B<3

I want to make a 2 dimensional string array and put it in this form
A B C D
>3 <3 >2 >5 <-- so this sample is a 2x4 array

Here is how I got it: First read through the first 4 row then put each of them in a column. Then read through other 2 row, check which character matches whichof the first 4 rows and put the comparison sign and number under it in the 2 dimensional array. So for example when I get to line"A>3;B>2" I check which column A is in and put >3 under it, when encounter a ";" move to check next one on the line, so check which column C is in and put >2 under it in the new two dimensional array... and so on. I don`t know how to write a java code to extract like what I wanted.

What I have done so far is I was able to get each row into a 1 dimensional string array each from reading the textfile in,
so now I got a string array to have such content:
text[0]=A
text[1]=B
.
.
text[5]=C>5;D<3

But I want to turn those text[] string array into the 2 dimensional string array form that I want. How do I extract content of each string array? How should I start?

First step: know exactly what assumptions you can make as far as the data file. I am assuming that the first part is a list of letters, one per line, then any line with anything else than a letter is part of the rest of the second part. Seems to me you can disregard any line WITHOUT a > or < sign, so read in a line at a time and if there is only a letter, toss it out. Once you get lines with < or >, you start keeping the data and parsing it. Your delimiter is a semicolon, so split the line into different segments delimited by a semicolon, and throw the semicolon out. String.split could be useful here. You now have strings like "A>3". You need to split that into "A" and ">3". You can use String.split again to find the '>' or '<' and split the string again. Then store the "A" and ">3" in your array.

In your example, you have a SINGLE dimension array called text. Have text be a 2-D array so that:

text[0][0] = "A";
text[0][1] = ">3";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.