7 Replies Latest reply on Mar 5, 2013 5:57 PM by 939520

# Assign values from one array to another

Hi, I am trying to solve a problem that asks that I assign values in the array that is input, which meet a certain criteria, into another array. isFunny will determine if a given integer is funny, countFunnies will determine how many integers in the given array are funny or not. collectFunnies should assign elements from the first array that are funny into the new array. Please take a look at the code, I am at a loss, everything looks good to me.

public class BatchOne
{

public boolean isFunny(int x) {
if (x%2 == 0) {return true;}
else {return false;}
}

public int countFunnies(int[] a) {
int count = 0;
for (int funCheck :a) {
if(isFunny(funCheck)) {
count++;
}
}
return count;
}

public int[] collectFunnies(int[] a) {
int count = 0;
int i = 0;
for (int funCheck :a) {
if(isFunny(funCheck)) {
count++;
}
}
int[] array = new int[count];
for (int funCheck : a) {
if(isFunny(funCheck)) {
array[i] = funCheck;
i++;
}
}
return array;
}
}

Thanks,

T
• ###### 1. Re: Assign values from one array to another
Welcome to the forum!
>
Hi, I am trying to solve a problem that asks that I assign values in the array that is input, which meet a certain criteria, into another array. isFunny will determine if a given integer is funny, countFunnies will determine how many integers in the given array are funny or not. collectFunnies should assign elements from the first array that are funny into the new array. Please take a look at the code, I am at a loss, everything looks good to me.
>
That's not how the forums work.

You need to either ask a question or present an issue. You haven't done either one.
• ###### 2. Re: Assign values from one array to another
Oh sorry about that. My questions is, what am I doing wrong with the assignment of values that are found to be funny into the new array?

Thanks,

T
• ###### 3. Re: Assign values from one array to another
You haven't posted anything that shows you are even executing the code, the results you are getting or a stack trace that shows that there is anything wrong.

The forum is not for code review. It is to help people that have a problem.

If there is a problem with the code post ALL of the code that is being used and tell us what results you are getting, what results you expect to get or a stacktrace that shows the code is not working and what line the problem is on.
• ###### 4. Re: Assign values from one array to another
Ok, I am new to programming and posting so likely need some refinement to my posts.

I am using BlueJ to write my code, the above posted is my entire code. It is compiling correctly but when I run, the return was giving me back something strange, I am not sure if I can post a screenshot because it would be hard to explain.

However, upon further review, it seems that there are some intricacies with BlueJ that I am obviously not used to, because when I select to inspect the return, the program seems to be returning an array of values that is in line with my intended goal.

Thank you for your guidance in terms of how to use the forums.

T
• ###### 5. Re: Assign values from one array to another
>
It is compiling correctly but when I run
>
As I said above
>
You haven't posted anything that shows you are even executing the code, the results you are getting or a stack trace that shows that there is anything wrong.
• ###### 6. Re: Assign values from one array to another
Hi,

I am not able to compile your program..Assigning int to an array is the problem..Fix your compilation issue..

Sankar.
• ###### 7. Re: Assign values from one array to another
It appears your problem is here:
change 'array = funCheck' to 'array[ i ]=funCheck'

for (int funCheck : a) {
if(isFunny(funCheck)) {
array = funCheck;
i++;
}