wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Question with CodingBat exercise: Array-2: has22 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question with CodingBat exercise: Array-2: has22" Watch "Question with CodingBat exercise: Array-2: has22" New topic
Author

Question with CodingBat exercise: Array-2: has22

Elvin Blen
Greenhorn

Joined: Mar 07, 2013
Posts: 2
Good afternoon fellow greenhorns,

I'd like to take a second to introduce myself (on top of the question of course); my name's Elvin and I'm an I.T. student at Florida International University. At the moment I am going through some issues learning java and would like someone's assistance in completing the problem. I already know the answer to the problem but that's not what I'm looking for, I want to understand why my code doesn't work as opposed to the code that I found online. Provided is a link to the problem so that you may all see it (basically, in an array, if there is a 2, directly next to another 2 [in the elements of the array] return true, else return false. Seems simple enough right? WRONG! Below is my code and below that is the code that works.

My code:


---------------------------

working code:


----------------
Someone please cure this ache of mine.... thanks in advance for all replies!
Elvin Blen
Greenhorn

Joined: Mar 07, 2013
Posts: 2
I seem to have forgotten to provide the link, here it is.

http://codingbat.com/prob/p121853
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2248
    
  47
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2248
    
  47
The difference between the 2 blocks of code is where the "return false;" line is. In your code it's inside the for loop so at the end of the first loop iteration it if it hasn't found a 2,2 the code returns false.

BTW in your code, once you fix the above problem, you will get an IndexOutOfBoundsException if the array doesn't contain consecutive 2's. You need to loop until i < array length - 1.

In future when faced with this type of problem add a print statement in the loop so you can see what is happening.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Elvin Blen wrote:I want to understand why my code doesn't work as opposed to the code that I found online.

ItDoesntWorkIsUseless (←click)

But let me guess... you get an IndexOutOfBoundsException, right? This indicates that you're trying to access an array element that doesn't exist. Check your code and identify which array index would be invalid at some point.

Edit: ah...I didn't notice the incorrect placement of the return statement that Tony pointed out.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
 
subject: Question with CodingBat exercise: Array-2: has22