Hello, I'm trying to write a simple program to finde words betwen statments like <int></int>.

I converted String stream to a charArray, to get indexes of chars in the string.

public void foo(){
    	
    	
    	bucket=str.toCharArray();

    	System.out.println(str.substring(548,563));
    	System.out.println(str.substring(644,656));
    	System.out.println(str.substring(844,857));
    	System.out.println(str.substring(942,954));
    	
  
   
    	System.out.println(str.length());
    	
    	
    	for(int i=0;i<str.length();i++){
    		if(bucket[i+0]==':' &&
    		   bucket[i+1]=='i' &&
    		   bucket[i+2]=='n' &&
    		   bucket[i+3]=='t' &&
    		   bucket[i+4]=='>' ){
    			index1=i+5;
    		   System.out.println(index1);  	
    		}
    		
    		if(bucket[i+0]=='/' &&
    		   bucket[i+1]=='i' && 
    		   bucket[i+2]=='n' && 
    		   bucket[i+3]=='t' && 
    		   bucket[i+4]=='>' ){
    			
    			index2=i-1;
                System.out.println(index2); 
                
    		}
    		
    		
    		
    		}
    System.out.println(str.substring(index1, index2));
    }

OUTCOME :

2567</type:int>
5</type:int>
34</type:int>
1</type:int>
986
548
563
644
656
844
857
942
954
java.lang.StringIndexOutOfBoundsException: String index out of range: -954

So, as you see:

System.out.println(str.substring(548,563));
System.out.println(str.substring(644,656));
System.out.println(str.substring(844,857)); WORKS !!!!!!!!
System.out.println(str.substring(942,954));
-------------------------------------------

System.out.println(str.substring(index1, index2)); DOESN'T WORK !!!!!

^^^^------ I need it, to work like this.


Dont know what work I'm doing with the substring.
Plz help.

2567</type:int>
5</type:int>
34</type:int>
1</type:int>

From your output, it looks like you are actually looking for <type:int></type:int> tags. If this is the case, then index1 is being set each time, and index2 is not being set. This is because, the match criteria for index1 is ":int>", which would match "<type:int>" and "</type:int>".

Also, shouldn't line 40 be inside the for loop? Otherwise you will only be outputting the last result.

I changed to :

for(int i=0;i<str.length();i++){
    		if(
    		   bucket[i+0]=='<' &&
    		   bucket[i+1]=='t' &&
    		   bucket[i+2]=='y' &&
    		   bucket[i+3]=='p' &&
    		   bucket[i+4]=='e' &&
    		   bucket[i+5]==':' &&
    		   bucket[i+6]=='i' &&
    		   bucket[i+7]=='n' &&
    		   bucket[i+8]=='t' &&
    		   bucket[i+9]=='>' ){
    			index1=i+10;
    		   System.out.println(index1);  	
    		}

                if( //<type:int>1</type:int>
    	                   bucket[i+0]=='<' &&
    	    		   bucket[i+1]=='/' &&
    		           bucket[i+2]=='t' &&
    	    		   bucket[i+3]=='y' &&
    	    		   bucket[i+4]=='p' &&
    	    		   bucket[i+5]=='e' &&
    	    		   bucket[i+6]==':' &&
    	    		   bucket[i+7]=='i' &&
    	    		   bucket[i+8]=='n' &&
    	    		   bucket[i+9]=='t' &&
    	    		   bucket[i+10]=='>' ){
    	    			index2=i-1;
                System.out.println(index2); 
                
    		}
    		
    		System.out.println(str.substring(index1, index2));
    		
    		}

But it still doesn't work

OutCome:

2567</type:int>
5</type:int>
34</type:int>
1</type:int>
986

...a lot of blank lines...
548
java.lang.StringIndexOutOfBoundsException: String index out of range: -548

Maby you guys have a better, faster solution to get values from betwen the statments

You really don't need to do this all one char at a time. The String class has all the methods you need working with whole strings at a time, eg

indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.

as in

index2 = str.indexOf("</type:int>");

Sorry, a little mistake while copying.
Thx for the link.

But there is stil problem with the substring.

Yes James youre right. But how do make it work if there are a lot of statmets like this. How to write each result.

Try changing lines 14 and 30 to System.out.println("Index 1: " + index1); and System.out.println("Index 2: " + index2); ; then re-post your output. I looks like, at least in the first couple outputs, index2 is staying at its default value (0).

Having said that, there are some flaws that need to be considered first. In this program, only the last set of tags will be printed to the output. In addition to this, if there is an open tag ( <type:int> ) with no closing tag, it will cause an error.

... But how do make it work if there are a lot of statmets like this. How to write each result.

Use indexOf(String str, int fromIndex)
initially look for the first opening tag (fromIndex = 0), then look for the first closing tag after that index, then look for the next opening tag after the first closing tag (etc)

int searchFrom = 0;
while (...
  startTagIndex = str.indexOf(openingTag, searchFrom);
  endTagIndex =  str.indexOf(closingTag, startTagIndex + 10);
// process substring between these 2 indexes
  searchFrom  = endTagIndex + 10;
}

[nitpick]
startTagIndex + openingTag.length();
...
searchFrom = endTagIndex + closingTag.length(); // adv ptr past last tag found
[/nitpick]

<Excuse>Yes, sure, actual length of the tags, not literal 10, but then I was just illustrating the idea without actually writing the OP's code for him </Excuse>

always you have check before if SubString cursor != or > myString.lenght()

Try changing lines 14 and 30 to System.out.println("Index 1: " + index1); and System.out.println("Index 2: " + index2); ; then re-post your output. I looks like, at least in the first couple outputs, index2 is staying at its default value (0).

nmaillet, you're right. The index2 stays 0 whole time. But why ?

OUTCOME:

2567</type:int>
5</type:int>
34</type:int>
1</type:int>
986
|
| ... a lot of blank lines ...
|
index1:548
index2 0
java.lang.StringIndexOutOfBoundsException: String index out of range: -548

Thanks James for help. I'll try it after i fix this problem because i'll going to need it anyway.

because i'll going to need it anyway

What for?
It's very improbable that you will find a search/replace etc that requires you to work at the char[] level. Just use the methods already implemented in String, and then move on to Regex for the really advanced stuff.

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.