Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Character value from a File

Status
Not open for further replies.

Creeder

Programmer
Jul 5, 2000
110
MY
Dear all,

Let's say i have a text file with the following line.

xxxxxtestxxxxxtest2xxxx (x represents a blank space)

How would I output the first word and ignore the second word?

Therefore the output shoud be 'test'.

I guess i would have to determine the length of the string, and check each character whether it is a character or space using isSpace() and isLetter(). But how do i ignore the 2nd word?

Any suggestion would be helpful :)

Creeder



[sig][/sig]
 
Someone will probably come along with something shorter and better but this works on my computer:

StringTokenizer st = new StringTokenizer(YOURSTRING," ");
String firstWord = "";
while (st.hasMoreTokens()){
firstWord = st.nextToken();
if (firstWord.length() > 0) break;
}

At this point the variable firstWord would be &quot;test&quot; [sig]<p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br> [/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top