• 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

OCA Study Guide Page 89 SearchSample.java

 
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:
Hello,
I'm having this problem when I try the SearchSample.java example from page 89 of the study guide. Running it from the command line gives me an error "SearchSample.java:60: error: class, interface, enum, or record expected
       }
       ^
1 error"

If I copy/paste the same program onto Eclipse, it runs but the output is (2, 0) which as explained in the text means that when the condition list[i][j] == searchValue is met, the program only exits the inner loop and continues to the third value of i (2). Attached are the relevant screen captures. Thanks in advance for any explanation or suggestion.
Screenshot-2024-01-26-200556.png
SearchSample.java
Screenshot-2024-01-26-200630.png
Command line error
Screenshot-2024-01-26-200957.png
Eclipse with output
 
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Your braces ({}) should be used in pairs, the error message is telling you that you have mismatched braces. Fix your indentation and you should find it.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Also, in Eclipse, if you put the cursor to the right of an open or close brace Eclipse will then highlight the matching one.
 
Alberto Richardson
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:
Hello,
Thank you for your replies.

Regarding the number of curly braces, Carey, you were right: the problem was that in Notepad, I had extra closing curly braces way down the screen, I had to scroll far down to see them as they wouldn't show on the top screen. That takes care of the compile error with the command line. But in Eclipse and via the command line, I am still getting the output Value 2 found at (2, 0) instead of the expected Value 2 found at (1, 1).
Here is the code:
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
If you print out the value of list[i][j] before you compare it to 2, you'll see why that is happening. Also print i and j.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Looks like "PARENT_LOOP:" should be between lines 8 and 9.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
The way you've got it now you're only breaking out of the inner loop but the outer loop continues.
 
Alberto Richardson
Greenhorn
Posts: 9
  • Number of slices to send:
    Optional 'thank-you' note:
Thank you all again; your feedback is extremely helpful.

Yes, my code was wrong and different from the book. Because I had the PARENT_LOOP label misplaced, I was breaking out of the inner loop. So the correct code is  just like it reads on the book:


 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
I would work on your indentation a bit. My personal preference is to put labels (PARENT_LOOP:) on a line by themselves, un-indented by one step.
 
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Alberto Richardson wrote:. . . in Notepad . . .

There are much better text editors, for example NotePad2 (which isn't related to Notepad). It supports bracket matching, automatic indentation, etc. There is also something called NotePad++, which is related to NotePad2, but even better, I personally think. I suggest you use that instead of NodePad. If you use an IDE, it will usually pair brackets, etc., but on a text editor do what I call writing backwards: see this old post.
 
Alberto Richardson
Greenhorn
Posts: 9
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Thank you both for the valuable recommendations. The newly indented program looks much better.
 
Ranch Hand
Posts: 49
1
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:I would work on your indentation a bit. My personal preference is to put labels (PARENT_LOOP:) on a line by themselves, un-indented by one step.



Great tip. I actually just did the same exercise yesterday and found the positioning of PARENT_LOOP clunky.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Tweaky stuff:
  • You had variables for 'X' and 'Y' positions. 'Y' is a vertical position as is 'row' yet you are assigning the 'col' position to 'Y' which can be confusing to  the reader. Better to have variables 'row' and 'col'.
  • I prefer to use printf() for print formatting, I think it's a bit cleaner and easier to read.

  •  
    Ranch Foreman
    Posts: 906
    8
    • Number of slices to send:
      Optional 'thank-you' note:
    Alberto and everyone,

    I didn't know about Java Labels.  I read up on them a little.  
    They can be placed in front of any statement including loops and switch statements.


    Thank you,

    Kevin
     
    Campbell Ritchie
    Marshal
    Posts: 79392
    377
    • Number of slices to send:
      Optional 'thank-you' note:

    kevin Abel wrote:. . . Java Labels.  I read up on them a little. . . .

    Did your source say whether labels are a good thing or not? I never use them.
     
    Chris Smithtopher
    Ranch Hand
    Posts: 49
    1
    • Number of slices to send:
      Optional 'thank-you' note:

    Campbell Ritchie wrote:

    kevin Abel wrote:. . . Java Labels.  I read up on them a little. . . .

    Did your source say whether labels are a good thing or not? I never use them.



    The example above is from Boyarsky and Selikoff, and iirc they don't take a particular stance on whether they're good or bad. More like "Here's a thing you might see, here's how it's used, but it's up to you".

    I don't know about Kevin's source on them, but the exam prep guide was my first encounter with them despite 2 semesters of Java.
     
    Paul Clapham
    Marshal
    Posts: 28258
    95
    • 1
    • Number of slices to send:
      Optional 'thank-you' note:

    Chris Smithtopher wrote:The example above is from Boyarsky and Selikoff, and iirc they don't take a particular stance on whether they're good or bad. More like "Here's a thing you might see, here's how it's used, but it's up to you.


    If feature X is defined to be one of the Java features covered by the exam, then you have to learn it. There's lots of features which are disliked by various people for various reasons (my personal dislike is having to learn the difference between x++ and ++x) but those dislikes are all irrelevant, they're on the exam so you have to learn them. After all in your future day job you might encounter one of those unlikeable features in code written by somebody else and you'd have to understand how it works.
     
    Chris Smithtopher
    Ranch Hand
    Posts: 49
    1
    • Number of slices to send:
      Optional 'thank-you' note:

    Paul Clapham wrote:

    Chris Smithtopher wrote:The example above is from Boyarsky and Selikoff, and iirc they don't take a particular stance on whether they're good or bad. More like "Here's a thing you might see, here's how it's used, but it's up to you.


    If feature X is defined to be one of the Java features covered by the exam, then you have to learn it. There's lots of features which are disliked by various people for various reasons (my personal dislike is having to learn the difference between x++ and ++x) but those dislikes are all irrelevant, they're on the exam so you have to learn them. After all in your future day job you might encounter one of those unlikeable features in code written by somebody else and you'd have to understand how it works.



    And frankly, I'd much rather have at least seen it and know it exists than running into it in the real world and going "HUH"
     
    Carey Brown
    Saloon Keeper
    Posts: 10779
    86
    • Number of slices to send:
      Optional 'thank-you' note:
    Without label...
     
    Carey Brown
    Saloon Keeper
    Posts: 10779
    86
    • Number of slices to send:
      Optional 'thank-you' note:
    Yet another way of breaking out of nested loops. This sometimes helps us refactor our code into smaller modules.

     
    Chris Smithtopher
    Ranch Hand
    Posts: 49
    1
    • Number of slices to send:
      Optional 'thank-you' note:

    Paul Clapham wrote:

    Chris Smithtopher wrote:The example above is from Boyarsky and Selikoff, and iirc they don't take a particular stance on whether they're good or bad. More like "Here's a thing you might see, here's how it's used, but it's up to you.


    If feature X is defined to be one of the Java features covered by the exam, then you have to learn it. There's lots of features which are disliked by various people for various reasons (my personal dislike is having to learn the difference between x++ and ++x) but those dislikes are all irrelevant, they're on the exam so you have to learn them. After all in your future day job you might encounter one of those unlikeable features in code written by somebody else and you'd have to understand how it works.



    I'm re-reading this section at the moment, since nested loops are a bit of a weakness for me, and it turns out the labels are NOT on the exam, but it looks like they use them kind of a lot in this section in order to help visualize and clarify how the loops work, and what will happen with a labeled and unlabeled break statement.

    On the one hand, it does help clarity a bit, but on the other hand, if it's not on the exam and most people don't use them, the amount of time spent on them seems a smidge excessive.

    Like I said before, though, better to know about them then be surprised irl.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic
    vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
    http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |