Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Help] Java: Save many strings to one 2d string array. How?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
PaddyW
Tux's lil' helper
Tux's lil' helper


Joined: 19 Jun 2006
Posts: 113
Location: germany/nrw/neuss

PostPosted: Thu Apr 15, 2010 9:39 am    Post subject: [Help] Java: Save many strings to one 2d string array. How? Reply with quote

Hey guys,
I'm pretty new to Java. Sorry for the bad thread name, but I'm just so helpless, I can't even find a proper name for my problem.
My problem is:

I'm reading a file, line by line. That's no problem. The thing is, the file contains a section where it says:

header
begin
this is gentoo linux
i am
paddyw noob
...
end

My code reads each line as a string-array. the delimiter is a whitespace. so 'header' is a string-array with length 1, and 'this is gentoo linux' is a string-array with length 4 and so on.

I want to create a 2D string array, where it saves each string.

array[0][0] = "header"
array[1][0] = "begin"
array[2][0] = "this"
array[2][1] = "is"
...

and so on. The problem is: How can I do that? All my strings have different lengths...

A friend of mine told me I should look up ArrayLists, because with ArrayCopy it seems pretty confusing. But it's hard to understand that without a proper example code or tutorial. Can someone help me please?

Thank you very much! If this is over I'm going to party!!!!!!
Back to top
View user's profile Send private message
Dr.Willy
Guru
Guru


Joined: 15 Jul 2007
Posts: 547
Location: NRW, Germany

PostPosted: Thu Apr 15, 2010 9:48 am    Post subject: Reply with quote

Code:
ArrayList lines = new ArrayList();
String line;
while((line = reader.readLine()) != null) {
    lines.add(Arrays.asList(line.split(" ")));
}


..from the top of my head. Convert to generics if you feel like it :)
Back to top
View user's profile Send private message
PaddyW
Tux's lil' helper
Tux's lil' helper


Joined: 19 Jun 2006
Posts: 113
Location: germany/nrw/neuss

PostPosted: Thu Apr 15, 2010 10:05 am    Post subject: Reply with quote

Thanks for the fast reply!!!

So, I have an object of type ArrayList. But what if I want to return an object of type String[][]?
Back to top
View user's profile Send private message
HoloDoc
Tux's lil' helper
Tux's lil' helper


Joined: 20 Jun 2006
Posts: 76

PostPosted: Thu Apr 15, 2010 11:51 am    Post subject: Reply with quote

Hey PeddyW!

here is my Solution for your 2 dimensional String Problem:

Code:
public static void main(String[] args) {
   ArrayList<String[]> lines = new ArrayList<String[]>();
   String[] inputArray = new String[1];
   inputArray[0] = "header";
   
   lines.add(inputArray);
   
   
   inputArray = new String[1];
inputArray[0] = "begin";
   lines.add(inputArray);
   
   inputArray = new String[2];
inputArray[0] = "this";
inputArray[1] = "this";
   lines.add(inputArray);
   String[][] sOut = ArrayListToPaddyWsGreat2DimensionalExcellentArray(lines);
   
   
   
   
}
public static String[][] ArrayListToPaddyWsGreat2DimensionalExcellentArray (ArrayList<String[]> alInput){
   String[][] out =new String[alInput.size()][];
   for(int i = 0; i < alInput.size(); i++){
      out[i] = alInput.get(i);
   }
   
   return out;
}


have fun!

Holodoc

ps: no violation against doves ;)
Back to top
View user's profile Send private message
Voltago
Advocate
Advocate


Joined: 02 Sep 2003
Posts: 2593
Location: userland

PostPosted: Thu Apr 15, 2010 12:02 pm    Post subject: Reply with quote

I'd try (similar to Dr. Willy)

Code:
BufferedReader br = new BufferedReader(new FileReader("myfile.txt"));
ArrayList<String[]> lines = new ArrayList();
String line;
while((line=br.readLine())!=null)
    lines.add(line.split("\\s*")); //splits each line around whitespace characters and adds to lines
String[][] linesArray = (String[][]) lines.toArray();


Note the last line. Problem with using a String[][] object directly is that you don't know how many lines you will need, so it's more convenient to use something dynamically growing like ArrayList.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum