has23

Hi,

i am trying

http://codingbat.com/prob/p171022

public boolean has23(int[] nums) {
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return true;
  
  else
  return false;
  }
  
  return false;
}

Open in new window


i failed one test


how to improve and fix my above code
please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
return nums[0]==2||nums[0]==3||nums[1]==2||nums[1]==3;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Your code fails because you break out of the loop at the first false result (thus returning false)
0
awking00Commented:
Another approach would be to convert the int[] to  list of Integer objects which can use the contains method
public boolean has23(int[] nums) {

ArrayList list = new ArrayList<Integer>();
            for (int i : nums) {
                  list.add(i);
            }
            if (list.contains(2) || list.contains(3)) {
                  return true;
            }
            return false;
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

krakatoaCommented:
how to improve an fix my above code


... again, you can read my comment here, because hard-wired reassignment of array values to new ones is fine for tiny arrays, but when dealing with larger arrays, it makes no sense to write verbatim code.
0
CEHJCommented:
The important thing here gudii, is to know how and where you went wrong. Are you clear on that?
0
gudii9Author Commented:
Your code fails because you break out of the loop at the first false result (thus returning false)

why it does that?

| should check other value also right(i am not using short cut OR like || which breaks as soon as it see first false?)

please advise
0
gudii9Author Commented:
[quote][quote]public boolean has23(int[] nums) {
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return true;
  
  
    if(nums[i]==3 | nums[i]==2)
  return true;
  
  else
  return false;
  }
  
  return false;
}[/quote][/quote]

Open in new window


i thought above code should pass all but failed. i wonder why. please advise
0
gudii9Author Commented:
return nums[0]==2||nums[0]==3||nums[1]==2||nums[1]==3;

Open in new window


i like this solutiin. How to write using for loop and int i etc to take make it more universal solution similar to my approach. please advise
0
ozoCommented:
public boolean has23(int[] nums) {
   int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
    if(nums[i]==2 | nums[i]==3) return true;
  }
  return false;

}

Open in new window

0
CEHJCommented:
to take make it more universal solution similar to my approach. please advise
But the problem domain is specific and the problem is not really reusable so universality, while a good aspiration often, is probably irrelevant here
0
ozoCommented:
Perhaps gudii9 now wants to talk about a different problem.
Maybe one that is similar to http://codingbat.com/prob/p171022 but without the array length 2 specification.
In which case, it may be helpful if we could get a clear description of the new problem.
If we do now want to talk about a different problem perhaps it help to avoid confusion about which question is being answered if the different problem is asked in a separate question.
0
gudii9Author Commented:
How my non working  below solution 


public boolean has23(int[] nums) {
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return true;
  
  else
  return false;
  }
  
  return false;
}



different from working solution


public boolean has23(int[] nums) {
   int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
    if(nums[i]==2 | nums[i]==3) return true;
  }
  return false;

}



i see my solution has extra return false; apart from that everything looks same to me. please advise

Open in new window

0
gudii9Author Commented:
public boolean has23(int[] nums) {
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return true;
  }
  else
  return false;
 
  
 // return false;
}

Open in new window


above gives below error


Compile problems:


Error:      else
      ^^^^
Syntax error on token "else", delete this token


why i cannot write like

else
 return false;

please advise
0
ozoCommented:
delete this token
public boolean has23(int[] nums) {
  int len=nums.length;
  
  for(int i=0;i<=len-1;i++)
  {
  if(nums[i]==2 | nums[i]==3)
  return true;
  }
  //else
  return false;
 
  
 // return false;
}

Open in new window

why i cannot write like
else is part of if...else
0
gudii9Author Commented:
so i can write if without else but i cannot write a method with if and then without return after if block right?

Is my understanding is correct?


 if i write a method with if (with return ) then else(with return) and then without return at the end of the method still the compilation error is thrown by compiler right?
0
ozoCommented:
else is part of if...else
there is no such thing as for...else in java
0
gudii9Author Commented:
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
//  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
//  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0];
  //return c;
  }
  
 else{
 return c;
  }
 
 //return c;
  
}

Open in new window


gives error

Compile problems:


Error:      public int[] front11(int[] a, int[] b) {
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
This method must return a result of type int[]

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.

see Example Code to help with compile problems

when i write as below
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
//  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
//  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0];
  //return c;
  }
  
 else{
 return c;
  }
 
 return c;
  
}

Open in new window


compiler is happy eventhough both else return and last return statement of method does same job in case of challenge(http://codingbat.com/prob/p128270) similar to this one
0
ozoCommented:
It may be better to discus this in
http://www.experts-exchange.com/questions/28691071/front11-challenge.html
but you could write as
public int[] front11(int[] a, int[] b) {
  int len1=a.length;
  int len2=b.length;
  
  int[] c=new int[(len1>0?1:0)+(len2>0?1:0)];
  
  if(len1>=1&len2>=1){  
  c[0]=a[0];
  c[1]=b[0];
//  return c;
  }
  
    if(len1>=1&len2==0){  
  c[0]=a[0];
  //c[1]=0;
//  return c;
  }
  
  
  
    if(len1==0&len2>=1){  
 // c[0]=0;
  c[0]=b[0];
  return c;
  }
  
 else{
 return c;
  }
 
// return c;
  
}

Open in new window

0
krakatoaCommented:
You can do this :

public boolean thingy(){

if(true){return true;}
else return false;

}

Open in new window


or this :

public static boolean thingy(){

if(true){return true;}
//else return false;
return false;

}

Open in new window


but not this :

public static boolean thingy(){

if(true){return true;}
//else return false;

}

Open in new window

0
krakatoaCommented:
And . . . I don't know what the others think about this, but IMHO you should ALWAYS frame if . . . else statements like this :

if( x ) { . . . ;}

else{ . . . ;}

USING curly braces EVERY TIME, even if you only use the if part.
0
awking00Commented:
I agree with krakatoa about the curly braces. I don't even like framing return values on the same line as the Boolean evaluation
if (true) {
return true;
} else {
return false;
}
0
ozoCommented:
USING curly braces EVERY TIME would be my personal preference.
I even tend to treat ){ and }else{ as two tokens, even though java treats them as five.
0
krakatoaCommented:
I don't even like framing return values on the same line as the Boolean evaluation

Yes, agreed.   Good point.    . . . and I admit to that being a bad habit of mine when I do stuff quickly, as here on EE. But it should not be in a real piece of code that way.

Ditto agreement with ozo's too.
0
gudii9Author Commented:
i got it about formatting. Also i got
You can do this :

public boolean thingy(){

if(true){return true;}
else return false;




or this :

public static boolean thingy(){

if(true){return true;}
//else return false;
return false;

}



but not this :

public static boolean thingy(){

if(true){return true;}
//else return false;

}
0
gudii9Author Commented:
public boolean thingy(){

if(true)

{
return true;

}
else return false;

}



above and below codes are technically same right? (even though way we write with or without else block?) please advise

public static boolean thingy(){

if(true)
{
return true;
}
//else return false;
return false;

}
0
krakatoaCommented:
above and below codes are technically same right? (even though way we write with or without else block?) please advise

Yes, the same.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.