Solved

array challenge error

Posted on 2014-11-01
13
237 Views
Last Modified: 2014-11-06
hi

When i am trying below
http://codingbat.com/prob/p136041

as

public boolean array123(int[] nums) {
for(int num : nums){
if (num==1 || num==2|| num ==3){
return true;
}
}
  
}

Open in new window

getting error


Compile problems:


Error:      public boolean array123(int[] nums) {
                     ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type boolean

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.


How to fix and improve. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40417511
The compile error can be avoided by adding a
 return true;
at the end of the function after the if block

You will still need to improve the actual result.
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 167 total points
ID: 40417816
I think you are not fully understanding what they are asking for in that question.

You need to check if the sequence 1,2,3 appears in the array.

Not if there is a 1 or a 2 or a 3 in the array.

Here's some code that checks for a sequence...but I've left it so it doesn't fully solve the problem.
Can you see how to adapt it to solve the problem?

You can add 2 lines to this and get the solution to the question, if you can figure out what 2 lines are needed...

Doug

public boolean array123(int[] nums) {
int target = 1 ;
for(int num : nums){
   if (num != target)
      target = 1 ;
      
   if (num == target)
     target++ ;
}

return false; 
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40417826
Interestingly, for the purpose of passing the tests at http://codingbat.com/prob/p136041, the
   if (num != target)
      target = 1 ;
does not seem to be necessary.
Although it could have been necessary if certain other examples had been used in those tests.
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 7

Author Comment

by:gudii9
ID: 40419792
public boolean array123(int[] nums) {
for(int num : nums){
if (num==1 || num==2|| num ==3){
return true;
}
}
  
}

Open in new window


by adding return i pass some tests and failing on some other as below

Expected      Run            
array123({1, 1, 2, 3, 1}) → true      true      OK         
array123({1, 1, 2, 4, 1}) → false      true      X         
array123({1, 1, 2, 1, 2, 3}) → true      true      OK         
array123({1, 1, 2, 1, 2, 1}) → false      true      X         
array123({1, 2, 3, 1, 2, 3}) → true      true      OK         
array123({1, 2, 3}) → true      true      OK         
array123({1, 1, 1}) → false      true      X         
array123({1, 2}) → false      true      X         
array123({1}) → false      true      X         
array123({}) → false      false      OK         


But they seem not mentioned about the sequence any where in challenge though. I wonder why,

How i improve my code to pass other tests too.
Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40419795
how to solve usinjg simple for loop and if condition rather than foreach loop. please advise
0
 
LVL 32

Accepted Solution

by:
awking00 earned 166 total points
ID: 40420015
I can see how you're getting confused about typical for loops and for-each loops as they are sometimes interchangeable and sometimes they are not, which is the case with this problem  In order to see if there is a 1 in the array followed by a 2 followed by a 3, you need to check 3 consecutive indexes of the array -
public boolean array123(int[] nums) {
  for (int j = 0; j < nums.length - 2; j++) {
    if (nums[j]==1 && nums[j+1]==2 && nums[j+2]==3) {
      return true;
    } else {
    return false;
    }
  }
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40420033
as they are sometimes interchangeable and sometimes they are not,

Can you please advise when they are not interchangeable?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40420036
public boolean array123(int[] nums) {
  for (int j = 0; j < nums.length - 2; j++) {
    if (nums[j]==1 && nums[j+1]==2 && nums[j+2]==3) {
      return true;
    } else {
    return false;
    }
  }
}

Open in new window


when i ran above code getting below error in the site
Compile problems:


Error:      public boolean array123(int[] nums) {
                     ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type boolean

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40420039
public boolean array123(int[] nums) {
  for (int j = 0; j < nums.length - 2; j++) {
    if (nums[j]==1 && nums[j+1]==2 && nums[j+2]==3) {
      return true;
    } 
  }
  return false;
}

Open in new window

All tests passed when i ran as above
0
 
LVL 84

Expert Comment

by:ozo
ID: 40421104
when they are not interchangeable?
Within a loop like
  for (int j = 0; j < nums.length - 2; j++) {
You can use j refer to an index into nums
Within a loop with
  for(int num : nums){
you can use num to refer to an element of nums

With a for(initialization; termination; increment) loop structure, you can change the initialization, the termination, and the increment.
If you want to change some of those things, this form may be preferable.
If you do not want to change those things, using the other form can help avoid mistakenly changing them.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40424375
If you do not want to change those things, using the other form can help avoid mistakenly changing them.

can you please elaborate on this
0
 
LVL 32

Expert Comment

by:awking00
ID: 40424414
I believe what ozo is trying to tell you is that with the example loop -
for (int j = 0; j < nums.length - 2; j++)
You don't have to start at index 0, you don't have to stop at any particular event, and you can increment (or decrement) by something other than one. With a for-each loop like (for (int num : nums), you are going to loop through every num that belongs in the group (array) of nums. You can't start or stop in the middle and you can't skip members of the group, but sometimes that's precisely what you want to occur and this format insures that. Note that is also insures no indexOutOfBounds exceptions.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40426495
Note that is also insures no indexOutOfBounds exceptions.
I love this as i got it so many times :)
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question