Advertisement

loop: for(....) {... continue loop; ....} doesn't work

Started by November 27, 2015 06:13 AM
2 comments, last by rip-off 9 years ago

To demonstrate the problem I have at the moment, consider this simple code. continue loop; discontinues and skips that iteration.


loop: for(int i= 0 ; i < 5; i++){
 if(i==2){
  continue loop;
 }
System.out.print(i);
}

will print

0134

For code below, area of interest -: loop: for ( ... ), continue. and the print code Log.v(...)

Basically, if "_rels/.rels" is found that loop should exclude the rest of that iteration and "i" should NOT be 0 at "after" print. But strangely, as the output shows, this didn't happen - the loop did not skip, why?

What code do I add to make sure when the String "_rels/.rels" is found the iteration jumps to the next? (not break; as i'm not looking to break out of the loop, I just need to skip that iteration and go to the next). Many Thanks


      public void unzip() { 
        try  { 
          FileInputStream fin = new FileInputStream(_zipFile); 
          ZipInputStream zin = new ZipInputStream(fin); 
          ZipEntry ze = null; 
          
          loop:for ( int i=0 ;(ze = zin.getNextEntry()) != null ; i++ ) { 
        	  
            Log.v(offset, "Unzipping before continue " + i + "  "+ ze.getName());
 
            if( ze.getName() == "_rels/.rels" ) continue loop; 

            Log.v(offset, "Unzipping after  continue " + i + "  "+ ze.getName()); 
            	
	            if(ze.isDirectory()) { 
	              _dirChecker(ze.getName()); 
	            } else { 
	              FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
	              for (int c = zin.read(); c != -1; c = zin.read()) { 
	                fout.write(c); 
	              } 
	
	              zin.closeEntry(); 
	              fout.close(); 
	            } 
      
          } 
          zin.close(); 
        } catch(Exception e) { 
          Log.e(offset, "unzip", e); 
        } 

    } 

output


11-27 04:06:21.073: V/offset(16671): Unzipping before continue 0  _rels/.rels
11-27 04:06:21.073: V/offset(16671): Unzipping after  continue 0  _rels/.rels

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

Seriously, stop hammering the Post button if it doesn't work right away.

This looks like Java. So the problem probably is the string comparison with the literal. Use this:

if ( ze.getName().equals( "_rels/.rels" ) ) continue loop;

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Advertisement

Many thanks.

I will try that now

(yeah, the site 's been so sticky lately)

Edit: forgot to update; if ( ze.getName().equals( "_rels/.rels" ) ) continue loop; Work perfectly well

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

Note that using == on Strings may appear to work sometimes, this is very likely due to how Java handles String *literals*, which are often de-duplicated to reduce memory usage. Strings loaded from an external source do not get this behaviour.

This topic is closed to new replies.

Advertisement