Click here to Skip to main content
15,851,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre>package dsa;
import java.util.ArrayList;
public class PairSumR {
	public static boolean isValidPair(int[] arr, int n, int k, int m) 
	{
	ArrayList<Integer> list = new ArrayList<Integer>();
		int rem= Integer.MAX_VALUE;
	for(int i= 0; i<arr.length;i++){
		
		 rem=arr[i]%k;
		 if(list.contains(m-rem)) return true;
		if(!list.contains(rem)) list.add(rem);
	} 
	
	return false;
	}
	
	public static void res() {
		System.setOut(isValidPair(arr, 5, 3, 2));
	}

	public static void main(String [] args) {
		
	
		
		int[] arr= {3,5,2,6,4};
		PairSumR.res();
		
	}
}


What I have tried:

i have passed the array in the method call and provided it as a parameter

error shown :
arr cannot be resolved to a variable
Posted
Updated 20-Mar-23 23:48pm
v3

arr is both a parameter to isValidPair and a local variable in main.
As such, it is only accessible inside those two methods - and because one is a parameter to a method, what's in it does not have to be the same array as declared in main as any array of integers can be passed in.

So it doesn't exist at all inside the res method - and that is what the error is telling you. Probably, you need it to be a parameter to the res method as well as the isValidPair method.
 
Share this answer
 
Comments
Shivam Siddharth 20-Mar-23 16:55pm    
why doesn't it consider it as an input since value is being given and its type is mentioned in the declaration
OriginalGriff 20-Mar-23 18:12pm    
Because it is out of scope.
Variables only exist while they are in scope - the curly brackets within which they are declared. Once they go out of scope they no longer can be accessed.
Shivam Siddharth 21-Mar-23 4:16am    
your thing worked ,but I am not using it outside its scope just passing the values like when called normally & how other variables are being accepted n and k
OriginalGriff 21-Mar-23 5:18am    
Because they are parameters as well - so they exist inside the isValidPair method, and not outside it.

Think of each method as a separate room in your house: if you want to get some milk, you need to go to the kitchen and open the fridge - it's not accessible from your bedroom! Similarly, if you want a shower, you have top go to the bathroom - there is no source of hot water in your lounge.

Scope matters in software as in real life: you need to be "in the right place" to use something.
Look at the call to the res method in main:
Java
public static void main(String [] args) {
    int[] arr= {3,5,2,6,4};
    PairSumR.res(); // calling res without any parameters

Now look at the call from res to isValidPair
Java
public static void res() {
    System.setOut(isValidPair(arr, 5, 3, 2)); // trying to pass the variable arr
}

But arr does not exist anywhere in res so the compiler rejects the code. You need to rework the definition of res and the call to it in main:
Java
public static void res(int[] arr) {  // the variable arr is received from the caller
    System.setOut(isValidPair(arr, 5, 3, 2));
}
public static void main(String [] args) {
    int[] arr= {3,5,2,6,4};
    PairSumR.res(arr); // calling res with the correct parameter

Also, I am not sure why you are using System.setOut here, as it expects a valid stream reference not a boolean.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900