Java String question

SinNisTeR

Diamond Member
Jan 3, 2001
3,570
0
0
i have a text file to read and it contains information relevant to the program. the text in the file is formatted such that it is separated by colons.

name : place
name : place
name : place
etc

basically, i would like to know if java has something built in that will let me get the name and place and store them into separate strings.. i thought of storing each line into a string and searching for the colon : and making a string of whatever was before and after and then locating the next line and doing the same thing for each subsequent line. but im hoping that java has some sort of method that will do this for me. :confused:
thanks for the help, this is really getting to me..
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: SinNisTeR
how would you assign one side to a string and the other side to another string? :confused:

It returns an array of strings.

The prototype from the docs: public String[] split(String regex)


String string = "name : place";
String [] strings = new String[0];
strings = string.split (" : ");
String string1 = strings[0];
String string2 = strings[1];
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Ummmm. Did you read the split() documentation? You call Split(":") on your string and you get an array of strings back. In your case, there will be 2 elements in the array.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: UCJefe
Ummmm. Did you read the split() documentation? You call Split(":") on your string and you get an array of strings back. In your case, there will be 2 elements in the array.

He tried to for 30 seconds, but couldn't understand it instantly, so instead of understanding it, he'll copy my code exactly and paste it into his assignment. Since he doesn't uderstand what my code does, he won't be able to modify it a little bit to fit the requirements of his homework, so he'll come up with some half-assed solution based around my code that sort of works, and he'll end up turning that in and getting a C on it.
 

jman19

Lifer
Nov 3, 2000
11,225
664
126
Originally posted by: notfred
Originally posted by: UCJefe
Ummmm. Did you read the split() documentation? You call Split(":") on your string and you get an array of strings back. In your case, there will be 2 elements in the array.

He tried to for 30 seconds, but couldn't understand it instantly, so instead of understanding it, he'll copy my code exactly and paste it into his assignment. Since he doesn't uderstand what my code does, he won't be able to modify it a little bit to fit the requirements of his homework, so he'll come up with some half-assed solution based around my code that sort of works, and he'll end up turning that in and getting a C on it.

LOL a painfully accurate assessment of the situation...
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: notfred
Originally posted by: UCJefe
Ummmm. Did you read the split() documentation? You call Split(":") on your string and you get an array of strings back. In your case, there will be 2 elements in the array.

He tried to for 30 seconds, but couldn't understand it instantly, so instead of understanding it, he'll copy my code exactly and paste it into his assignment. Since he doesn't uderstand what my code does, he won't be able to modify it a little bit to fit the requirements of his homework, so he'll come up with some half-assed solution based around my code that sort of works, and he'll end up turning that in and getting a C on it.

That's especially unfortunate when you consider that your code wouldn't even compile. I don't think you can even get a C for code that doesn't compile.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: kamper
Originally posted by: notfred
Originally posted by: UCJefe
Ummmm. Did you read the split() documentation? You call Split(":") on your string and you get an array of strings back. In your case, there will be 2 elements in the array.

He tried to for 30 seconds, but couldn't understand it instantly, so instead of understanding it, he'll copy my code exactly and paste it into his assignment. Since he doesn't uderstand what my code does, he won't be able to modify it a little bit to fit the requirements of his homework, so he'll come up with some half-assed solution based around my code that sort of works, and he'll end up turning that in and getting a C on it.

That's especially unfortunate when you consider that your code wouldn't even compile. I don't think you can even get a C for code that doesn't compile.

There, I fixed it just for you.
rose.gif
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
It's still redundant. It should say:

String[] strings = string.split(" : ");

Alright, I'm being anal. Everything else you've said in this thread has been spot-on.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
It's still redundant. It should say:

String[] strings = string.split(" : ");

Alright, I'm being anal. Everything else you've said in this thread has been spot-on.

Yep.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
MAN...I wish I knew of this split method a week ago. Although it was easy I spent like 30 minutes writing a method of embedded loops and conditional statements to parse strings from my config file for an application I am writing. Oh well, I guess I should start to actually read the java API.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: rainypickles
i used to use StringTokenizer, but i'm not sure if it was just because i was lazy. it worked for me.

That returns a new token each time you call it which means it needs to be in a loop. Since this will only have two tokens, it's kind of overkill to use that.
 

SinNisTeR

Diamond Member
Jan 3, 2001
3,570
0
0
:disgust::roll:
wow, some very educated statements notfred... no one forced you to post. it seems you posted for the sole purpose of crapping.. no need to be arrogant. After all, i thought, hey, this is the "SOFTWARE - APPS, PROGRAMMING AND GAMES" section of the forums. im sorry im not very versed in the java language. next time ill wait till im perfect in it to ask a novice-like question. :thumbsup:
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
At 4:53, I posted the link to the documentation.

At 4:56 you said you couldn't understand the documentation.

That means that you spent a MAXIMUM of 3 minutes looking at the documentation before posting again.

The third word of the link I posted is the return type of that method. It's right there on the socond line, just below the title. You obviously didn't get that far into the documentation before giving up, but hey, you were in a hurry obviously since you couldn't spare more than three minutes to try and read it.

If you ask a question, and I give you the right answer, and you don't even read it and then complain that you don't understand it, then yes you will get hostile replies in your thread.