Author |
Message |
Genisis
|
Posted: Mon Apr 10, 2006 8:22 am Post subject: How do i read one word from a datafile instead of the line? |
|
|
i know how to read a line from a datafile but how do i read just on word from the file ?any help is appreciated thanks |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Andy
|
Posted: Mon Apr 10, 2006 8:24 am Post subject: (No subject) |
|
|
what library are you using for input? |
|
|
|
|
|
MysticVegeta
|
Posted: Mon Apr 10, 2006 1:39 pm Post subject: Re: How do i read one word from a datafile instead of the li |
|
|
Genisis wrote: i know how to read a line from a datafile but how do i read just on word from the file ?any help is appreciated thanks
code: | if (inLine.indexOf(" ") < 0) singleWord = inLine;
else {
String [] sp = inLine.split (" ");
singleWord = sp[0];
} |
Note: Untested |
|
|
|
|
|
Andy
|
Posted: Mon Apr 10, 2006 3:52 pm Post subject: (No subject) |
|
|
errrr that's not what he wants.. you're reading the entire line, he wants to just read one token. |
|
|
|
|
|
MysticVegeta
|
Posted: Mon Apr 10, 2006 5:52 pm Post subject: (No subject) |
|
|
yeah but why cant we just read a line then break it down? Anyways, is it possible to read just 1 word? |
|
|
|
|
|
wtd
|
Posted: Mon Apr 10, 2006 7:38 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: yeah but why cant we just read a line then break it down? Anyways, is it possible to read just 1 word?
You certainly can read a line and then split it on a given delimiter (like " "). You will want to research "regular expressions".
If you have the ability to use Java 1.5.0.x you will want to research the Scanner class. |
|
|
|
|
|
Genisis
|
Posted: Sat Apr 22, 2006 11:30 pm Post subject: (No subject) |
|
|
i just want to read the first word then store it in an array then store the word after that in the same line so on and so forth and if you can how do you read characters from a data file |
|
|
|
|
|
Spastic
|
Posted: Sun Apr 23, 2006 12:10 pm Post subject: (No subject) |
|
|
string.substring(string.indexOf("a char"), string.indexOf("another char"));
or sumthin like that might work? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheFerret
|
Posted: Thu Apr 27, 2006 3:12 pm Post subject: (No subject) |
|
|
Genisis wrote: i just want to read the first word then store it in an array then store the word after that in the same line so on and so forth and if you can how do you read characters from a data file
The best way to do this is what MysticVegeta said and use foo.split("\\s") and it stores each word in the array sepertally... |
|
|
|
|
|
seymournexus
|
Posted: Thu Jun 08, 2006 11:38 pm Post subject: (No subject) |
|
|
Personally, I would read the line, and than use a string tokenizer (using spaces as the seperator) and get that first word, save it into a variable, and do whatever you need to do with it. hope this helps |
|
|
|
|
|
|