Link to home
Start Free TrialLog in
Avatar of gla
gla

asked on

transfer text to string array

I have a text like:

first
second
third

(with return)

and i want to transfer to a String array like:

String[] choices = {"first", "second", "third"};
Avatar of Mick Barry
Mick Barry
Flag of Australia image

String choices = s.split("\n\r");
Read into a List then turn that into String[]
sorry typo, that should be:

String[] choices = s.split("\n\r");
Avatar of gla
gla

ASKER

the text is in a jTextbox is this problem?
Avatar of gla

ASKER

sorry jTextarea...
Try

String[] tokens = ta.getText().split("\\s");
> the text is in a jTextbox is this problem?

no, just get the sytring using the textarea getText() method
Avatar of gla

ASKER

I have try with .split("\\s"); and .split("\n\r");

String [] to = jTextAreaTo.getText().split("\\s");

but not token
>>but not token

What do you mean by that?
Avatar of gla

ASKER

not split
I don't understand. Are there always single words per line?
Avatar of gla

ASKER

yes
> I have try with .split("\\s"); and .split("\n\r");

what I suggested should work fine.
you don't want to use \\s as it will split on more than just new lines.
print out the text before you split it to verify the new lines are there
Avatar of gla

ASKER

I have the following text for example:

1
2
34
liolios
geo lio

(different lines) in a jtextarea
and I want to put in a jlist with 5 lines but first must I put in String array is this clear
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gla

ASKER

the complete java code is the follow:

 javax.swing.DefaultListModel listModel = new javax.swing.DefaultListModel();

        String [] cc = jTextAreaCC.getText().split("\n\r");
       
        System.out.println(jTextAreaCC.getText());
       
        System.out.println(cc.length);
        listModel.removeAllElements();
       
        for (int i=0; i<cc.length; i++){
            listModel.addElement(cc[i]);            
        }
        jListCC.setModel(listModel);

and the result from previous example is one line in jList:
1234lioliosgeo lio
Avatar of gla

ASKER

yes objects working with:

textArea.getText().split("[\n\r]");


thanks
>>
yes objects working with:

textArea.getText().split("[\n\r]");

>>

IOW it's working with the code *I* posted ;-)

( [\n\r] is the same as [\r\n] )
Avatar of gla

ASKER

yes but if your eyes is open then you will see the differed between the "\n\r" and "[\n\r]" the differed is the "[]" ok?

my english is not good but the java language is international!
Avatar of gla

ASKER

CEHJ

I am realy sorry for my mistake yes you are right my eyes is not open

sorry very sorry!!!!

I correct this in the future!!
>>the differed is the "[]" ok?

Precisely - and that is the difference that *my* code example introduced
>>I correct this in the future!!

You could correct it now if you wanted - all i'd need to do is to get the question reopened ..;-)
Avatar of gla

ASKER

yes,
sorry again the problem is with me...
Avatar of gla

ASKER

ok I repoen (new one)
You don't need to post a new question. Delete it please ;-). I'll get this reopened if you want
Avatar of gla

ASKER

ok
Have requested it be done - you don't need to do anything yet, other than to delete that new Q
Avatar of gla

ASKER

ok I deleted the new Q
Thanks
Avatar of gla

ASKER

CEHJ

I think I correct my mistake, sorry again...
:-)
No problem
Well they were posted at the same time, and I originally suggested using split().
He suggested reading into a List and then once he saw my suggestion he's changed his tune without any mention that I actually originally suggested it. Please reopen and split the points please.
>>then once he saw my suggestion he's changed his tune

My 'changing my tune' had nothing to do with anything you posted. It was based on gla's post beginning

>>I have the following text for example:
Avatar of gla

ASKER

If you are agree (objects and CEHJ) and I agree to split the points

for my problem the correct comment is the CEHJ that posted at 04/28/2006 02:23PM EEST erlier than objects comment posted at 04/28/2006 02:25PM EEST 2 min later.

I am sorry objects the best way for all of us will was to split the points but I make a mistake against the CEHJ and I find a way to correct it.
>>If you are agree (objects and CEHJ) and I agree to split the points

That's entirely up to you, but it's not customary to give points for incorrect answers or answers that are the same as ones posted previously