about string's functions

Hi,
is there any function gives the next position off substring. I mean function
indexOf("Name") gives first position of NAME. But I have text file with:
NAME 1---- VALUE
NAME 2---- VALUE
---------------------
NAME N---- VALUE .... I want get all indexs of NAME and put its in massive so I can manipulate it? Please help
sisquAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lhankinsCommented:
there's an overload of String.indexOf which takes a second parameter which is "where to start the search from".

see:

   http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#indexOf(int,%20int)

If you're trying to parse a file like that, you might consider the java.util.StringTokenizer class.     Is there a separator between name1 and name2...?   For example :

      String someString = "name1=value1;name2=value2;name3=value3";

      StringTokenizer toks = new StringTokenizer(someString, ";");

      String nvPair = null;
      String name = null;
      String value = null;

      while (toks.hasMoreTokens())
      {
         nvPair = toks.nextToken();

         int equalsIndex = nvPair.indexOf("=");
         name = nvPair.substring(0, equalsIndex);
         value = nvPair.substring(equalsIndex+1);

         System.out.println("name = [" + name + "] value = [" + value + "]");
      }

0
CEHJCommented:
You need to read the file and collect the indexes:

http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

Take the length of each String read, keep the total and subtract the index given to you by indexOf. You can keep each index in a List:

List indices = new ArrayList();
...

// (on each read line)

indices.add(new Integer(totalLength - str.indexOf("NAME")));
0
objectsCommented:
Sounds like you be better off storing the file as a list of lines

ArrayList lines =- new ArrayList();
BufferedReader in = new BufferedReader(new FileReader("myfile.txt"));
String line = null;
while (null!=(line=in.readLine()))
{
   lines.add(line);
}

you can then just manipulate each line individually.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

CEHJCommented:
Since it's 'massive' it would take up a lot of memory if the Strings are stored in memory. Plus it appears that what is required is to know the offsets
0
objectsCommented:
Theres nothing been mentioned to suggest that.
0
objectsCommented:
You may also be able to process the file as you read it depending on your requirements.
0
CEHJCommented:
>>Theres nothing been mentioned to suggest that.

What do you think the word 'massive' refers to then?
0
objectsCommented:
It doesn't mean anything, you're just making assumptions (again).
0
CEHJCommented:
>>It doesn't mean anything

Ah, so you think sisqu is making meaningless statements then?
0
objectsCommented:
:-D  No I think you are.
0
CEHJCommented:
So what was the 'massive' all about then sisqu?
0
objectsCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.