Jump to content
  • Advertisement
Sign in to follow this  
grumpyOldDude

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

This topic is 1027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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;

Share this post


Link to post
Share on other sites

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

Edited by alwaysGrey

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!