• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help understanding example from Oracle documentation

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm new to java and I'm learning about the Continue statement. I read about it on this link here http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html but the example that it provides, just doesn't make sense to me. I was hoping someone can go through the code and explain the reason behind this design, so that I can properly understand and learn how to implement similar solutions in the future.

Per the link

The following example program, ContinueWithLabelDemo, uses nested loops to search for a substring within another string. Two nested loops are required: one to iterate over the substring and one to iterate over the string being searched.



I've added comments on the code so that you can see what my questions are and what I don't understand.



I know this is not the usual question but it's killing me. If I want to properly learn and be a good programmer, I need to be able to understand this design. Any help is very much appreciated!
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
I think I shall have to go back to your post and break the long lines, then I shall answer the question.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you can actually see it on one screen
Some people would give you a very simple answer to when to use continue: never. It breaches the conventions of structured programming and can be confusing.
Other people happily use it.

My, that example is difficult to understand, isn't it! I am not surprised you are finding it complicated.
What you are trying to do is manipulate pointers. You cannot directly access the location of something in memory in Java® but you can tell that the 4th letter in a String is 3 places farther on in memory than the 1st letter. That is why you use text.charAt(3) to get the 4th character.
Actually it isn't 3. Each char occupies 2 bytes, so it is actually 6 places on. If you have ever done any C coding, that sort of thing is very common. I use that sort of code all the time, but in FORTH, which is even harder to understand.

Now, back to the Java®
They are trying to find a 3‑letter String inside a longer String (25 characters, I think) so you cannot start farther along than after the 22nd character. If you pass 23 and haven't found the three letters, you never will.
The idea of the continue is that you start at letter i and then move forward 1. For the bit about (n-- != 0) you need to go through it with a pencil. Let's start from 3:
(n-- != 0) That is true
(n-- != 0) That is true
(n-- != 0) That is true. You have decremented n thrice, but the whole expression n-- has the old value 1, so it still is not equal to 0.
(n-- != 0) Although n is now −1, the whole expression n-- has the old value 0 so the loop does not continue.

What you are doing is looking for a difference in the characters. If any of the characters is different, then control passes back to “test:”. Since “test:” is outside the outer loop, then control passes back to the outer loop, but it starts where it left off. It does not start from the very first iteration.
As for j++ and k++: again those increment their variables but the increment is not noticed until the next loop. If k is 0 then k++ increments k but returns the old value of k = 0.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never said it was easy, did I?

I think it is a good argument against using continue
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for taking the time to explain. I will go through this later when I have more time and see if I can understand it. That said, I refuse to think this is the best and most efficient way of doing this? I'm a beginner but I know java has a lot of libraries and built in methods, etc. Surely there has to be a more efficient way of finding a string within a string, no? I will also do some research on that myself.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is a good example of trying to find the most difficult way to do something.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that's a bad to solve the problem. It's also a good example of why I'm not a fan of the Oracle tutorials. I just don't think they are written at a beginners level. They were written by people so knowledgeable about Java that they find it impossible to come down to the level of a real beginner.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that bit was written when the Java people had just come out of C/C++ and hasn't been updated since.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic