gudii9
asked on
array challenge error
hi
When i am trying below
http://codingbat.com/prob/p136041
as
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
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;
}
}
}
getting errorCompile 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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public boolean array123(int[] nums) {
for(int num : nums){
if (num==1 || num==2|| num ==3){
return true;
}
}
}
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
ASKER
how to solve usinjg simple for loop and if condition rather than foreach loop. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
as they are sometimes interchangeable and sometimes they are not,
Can you please advise when they are not interchangeable?
ASKER
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;
}
}
}
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.
ASKER
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;
}
All tests passed when i ran as above
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.
ASKER
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
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.
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.
ASKER
Note that is also insures no indexOutOfBounds exceptions.I love this as i got it so many times :)
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.