?
Solved

about string's functions

Posted on 2004-11-28
12
Medium Priority
?
232 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:sisqu
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:lhankins
ID: 12691561
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12691666
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12691741
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12691759
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
 
LVL 92

Expert Comment

by:objects
ID: 12691830
Theres nothing been mentioned to suggest that.
0
 
LVL 92

Expert Comment

by:objects
ID: 12691836
You may also be able to process the file as you read it depending on your requirements.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12691873
>>Theres nothing been mentioned to suggest that.

What do you think the word 'massive' refers to then?
0
 
LVL 92

Expert Comment

by:objects
ID: 12691917
It doesn't mean anything, you're just making assumptions (again).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12691983
>>It doesn't mean anything

Ah, so you think sisqu is making meaningless statements then?
0
 
LVL 92

Expert Comment

by:objects
ID: 12691993
:-D  No I think you are.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12710126
So what was the 'massive' all about then sisqu?
0
 
LVL 92

Expert Comment

by:objects
ID: 12710690
:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question