are there any codes whereby it can read the first and last word of all the lines of a text file?

I would need to read the first and last word of text files and make sure both the first and last words matches the conditions then print the lines that matches the conditions and save them into a new file.

Your help is much appreciated.

Hi yap 1991,

Can you give a sample code of what you have done thus far and we can take it futher?

Try this to get d first word of a line

String firstWord;
int spaceIndex =  s.indexOf(' ');
if(spaceIndex == -1)
  firstWord = s;
else
  firstWord = s.substring(0,spaceIndex);

Then use same logic to get the last word (use newline char instead of ' ').
Store the line numbers which satisfy your condition. You can read the file again and display only those lines.
Get the idea?

Here is a simple way but not very efficient due to space required.

String[] words = lineString.split("\s+");  // use white space at least 1 between words
int wordSize = words.size();
String firstWord="", lastWord="";
if (wordSize>0) { firstWord = words[0]; }
if (wordSize>1) { lastWord = words[wordSize-1]; }

hi taywin so I can assign firstword = ATOM and lastword != H? is it possible for me to do that? and I have a question, how do i show the output? System.out.println(linString.split);

hi taywin so I can assign firstword = ATOM and lastword != H? is it possible for me to do that? and I have a question, how do i show the output? System.out.println(linString.split);

@yap_1991
Instead of reading the first and last word, read the entire line and match it to your REGEX as I have mentioned in this thread.

And again, here.
Closing both this thread and the referenced (in this post) thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.