-1

I declared the array before the for loop and now I want to concatenate the result of my for loop into the array. The following doesn't work and gives me error as "error: cannot find symbol".

My code is

for(int j=i; j<=arr.length-1; j++){             

    // checking for condition
    if (i<j )
    {
        int temp = arr[i]+arr[j];
        if (temp%sum==0) {
        System.out.println("Pair with given sum " +
                            sum + " is (" +temp+")");
            result[] += temp;
        }
    }
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
aditya
  • 339
  • 2
  • 12
  • sorry i forgot to include in the foor loop, it comes just before the if statement – aditya May 29 '16 at 05:58
  • 3
    `result[] += temp` is patently incorrect. Where was this array originally declared? – Makoto May 29 '16 at 06:04
  • Declare result[] array and insert temp to some index of the array. For example result[i] = temp. You can use an arraylist instead if you dont know what would be the final size of the result array. – Gihanmu May 29 '16 at 06:05
  • yes Makota i had declared "int result[]" – aditya May 29 '16 at 06:08
  • okay Gihanmu i willl try that – aditya May 29 '16 at 06:08
  • can you explain how to use an array list?/ – aditya May 29 '16 at 06:10
  • You will never get into the if statement.Inside the for loop you initialize j = i and then you check if i < j, which will always be false.Then result[] += temp is not valid because you do not specify the index of the array. – theVoid May 29 '16 at 06:11
  • elaborate your question when you are posting. you did not mention in your question that "i need to add all the temp value in an array so that i could print out the count of that array.." , if you post it in your question , then it would be more easy to help you. – yash May 29 '16 at 07:56

4 Answers4

2

You error is here

result[] += temp;

You need to provide an index to your array where it can store the value

Try something like this

result[i] += temp;

Also note that this will create an array with different temp values.

Plus in your for loop, you are doing j=i and then checking

if(i<j)

So your loop will run after 1 iteration, since the first time the condition will be false.

If you don't have to print the value temp and only the number of values, then you don't need an array at all.

You can simply create a variable

int count=0;

and then change your for loop to something like this

if (temp%sum==0) {
        System.out.println("Pair with given sum " +
                            sum + " is (" +temp+")");
            count++;
        }

Then print count.

If using an array is mandatory, you can print the length of array like

result.length

Hope this helps :)

Aradhna
  • 973
  • 10
  • 20
  • hi @aradhana thanks for the help, but the thing is i need to add all the temp value in an array so that i could print out the count of that array – aditya May 29 '16 at 06:44
  • So you mean that you need to store (not add) temp values in an array and then at least you want to print the number of items stored in the array? – Aradhna May 29 '16 at 06:54
  • yes i want to store in temp values to array and print the number of items stored – aditya May 29 '16 at 07:01
  • please check the edit. Also, please mark this as the answer if it helped you. :) – Aradhna May 29 '16 at 07:07
  • the count did help me...thanks a lot :) but still storing in array hasn't worked...i don't understand how to index my array. – aditya May 29 '16 at 07:13
  • Your problem is solved for now. But if you want to learn about array, please join me here http://chat.stackoverflow.com/rooms/112150/android-queries – Aradhna May 29 '16 at 07:26
0

your code be something like this.. mention index of result in loop and declare it before use. click here to see why that error occur.

can you provide int i value ,so that we can help you in looping.

int result = new int[100] //give value according to your program.
for(int j=i; j<=arr.length-1; j++){             
// checking for condition
if (i<j )
{
    int temp = arr[i]+arr[j];
    if (temp%sum==0) {
    System.out.println("Pair with given sum " +
                        sum + " is (" +temp+")");
        result[j] += temp;
    }
}    `
Community
  • 1
  • 1
yash
  • 2,101
  • 2
  • 23
  • 32
  • this still gives me an error as "error: cannot find symbol int result = new result[100];" – aditya May 29 '16 at 06:48
  • sorry but i only need indexes for the elements stored by the loop....thecode you havewritten will just increase the indexes. – aditya May 29 '16 at 09:53
0

Arrays do not work as you probably think.When you create an array like this

int x[10] = new int[10]; 

you can access the elements of the array like this: for example if you want the 1st element x[0] the second x[1](you statrt counting from 0 to the array length - 1).So what you have done here :

result[] += temp;

does not make sense.You sould always specify the index of the element that you wanna access(you probably want something like result[index] += temp;).

I hope i helped.

yash
  • 2,101
  • 2
  • 23
  • 32
theVoid
  • 743
  • 4
  • 14
0

Your error is at line

result[] += temp;

You need to mention the array index in which you want to concatenate the result. For eg

//your code goes here
.....
for (int k = index_of_resultArray_to concatenate; k < result.length; k++) {
    result[k] += temp;
}
Tarang Bhalodia
  • 1,185
  • 11
  • 21