# front11 challenge

Hi,

I am trying below challenge

http://codingbat.com/prob/p128270

``````public int[] front11(int[] a, int[] b) {
int len1=a.length;
int len2=b.length;

int[] c=new int[2];

if(len1>=1&len2>=1){
c[0]=a[0];
c[1]=b[1];
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[1]=b[1];
return c;
}

else{
return null;
}

}
``````

i failed tests.

when to say
c[0]=0;
when to say
c[0]==0;

how to fix and improve my code and alternate ways of doing it. please advise
LVL 7
###### Who is Participating?

x
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.

Commented:
say c[0]=0; when you are setting c[0] to 0
say c[0]==0 when you are testing whether c[0] is 0
Commented:
public int[] front11(int[] a, int[] b) {
int lena=a.length;
int lenb=b.length;

int[] c=new int[(lena>0?1:0)+(lenb>0?1:0)];

if(lena>=1&lenb>=1){
c[0]=a[0];
c[1]=b[0];
}

if(lena>=1&lenb==0){
c[0]=a[0];
}

if(lena==0&lenb>=1){
c[0]=b[0];
}

return c;
}

Experts Exchange Solution brought to you by

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

Commented:
``````public int[] front11(int[] a, int[] b) {
return a.length==0&&b.length==0?new int[]{}:a.length==0?new int[] {b[0]}:b.length==0?new int[]{a[0]}:new int[]{a[0],b[0]};
}
``````
Commented:
Here's another, short "if" arrangement :

``````public int[] front11(int[] a, int[] b) {
if (a.length+b.length==0){return new int[]{};}
if(a.length==0){return new int[]{b[0]};}
if(b.length==0){return new int[] {a[0]};}
return new int[]{a[0],b[0]};
}
``````
Commented:
. . .  and in this bit of your code :

``````else{
return null;
}
``````

you are returning 'null'. But the challenge asks you to return an int array  - so why are you returning 'null'? You must return the required type. If the product of one of your code passages holds no values, then return an empty container of that type - in this case an empty int[].
Author 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[1]=b[1];
return c;
}

else{
return null;
}

}
``````

i modified as above and failing few tests. please advise where i am making mistake
Author 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[1]=b[0];
return c;
}

else{
return null;
}

}
``````

i corrected below highlighted
// c[0]=0;
c[1]=b[0];
return c;

Show output only (no red/green)
prev  |  next  |  chance   |  CodingBat  >  Array-1
Expected      Run
front11({1, 2, 3}, {7, 9, 8}) → {1, 7}      {1, 7}      OK
front11({1}, {2}) → {1, 2}      {1, 2}      OK
front11({1, 7}, {}) → {1}      {1}      OK
front11({}, {2, 8}) → {2}      {xception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:23}      X
front11({}, {}) → {}      {ul}      X
Author Commented:
i corrected further as highlighted

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 null;
}

}

still failing one test case

front11({}, {}) → {}      {ul}      X
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]; //  c[1]=b[1];
return c;
}

else{
return c; //return null;
}

}
``````
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 new int[]{};
}

}
``````
Author 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]; //  c[1]=b[1];
return c;
}

else{
return c; //return null;
}

}
``````

above solution worked but it has shaken my wrong concept that 'method needs to have separate return statement apart from if block return statement and else block return statement'
Commented:
Commented:
The correct concept is that all paths need a return
(unless the method is declared void)
###### 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.