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!

extracting text from notepad files-help? 1

Status
Not open for further replies.

KenWan

Programmer
Feb 27, 2004
14
GB

hi all. um is there a way to extract the values 1 and 2 from a text file containing
[value1][value2]
[another value1][another value 2]
and get rid of those stupid [] brackets?
 
Code:
		String s = "[aaa][bbb]";
		String[] sa = s.split("\\[");
		for (int i = 1; i < sa.length; i++) {
			System.err.println(sa[i].replaceAll("]", ""));
		}
 
So I guess my code would look sort of like this?
Code:
BufferedReader inFile = new BufferedReader(
                new FileReader("schools.txt") );
String line
while (inFile.ready())
  line = inFile.readLine();
  if (line.length() != 0){
        String[] val1 = line.split("\\[");
        for (int i = 1; i < val1.length; i++) {
            System.err.println(val1[i].replaceAll("]", ""));
             
        }
        //getting the second val after the space in [] []
        String[] val2 = line.split("\\ ["); 
        for (int k = 1; < val2.length; k++){
           System.err.println(val2[k].replaceAll("]"));
        } 

}

Um I guess, i'll give it a shot :) thanks again Sedj
 
I wouldn't do it like that, because the regular expression "\\[" is used to escape the "[" character, so doing "\\ [" will break the regular expression. Can you post some actual examples of hwat the lines will look like ...

 
um and example would be
[Primary School] [1]
[Secondary School] [2]
[University] [3]
[Incomplete Primary School] [1]
I've tried using the code that I made but I got errors the following errors.

The method split(String) is undefined for the type String.
The method replaceAll(String)(String) is undefined for the type String.

 
Ahhh, those methods for String are only in SDK 1.4, so I guess you are using 1.3 or below.

The below code will do, in effect, the same thing :

Code:
		String s = "[Secondary School] [2]";
		StringTokenizer st = new StringTokenizer(s, "[]");
		while (st.hasMoreTokens()) {
			String part = st.nextToken();
			if (part.trim().length() != 0) {
				System.err.println(part);
			}
		}
 
Thanks again Sedj, I'll give the code another try tommorow and report back with the results :)
 
I gave up using a stringTokenizer and got a substring instead. I'm using the indexOf for both the [] brackets and take the substring between then and squeeze it into an array,it works for now so im happy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top