Hello, in this code I am basically trying to call the Addition method into my ma
ID: 3796507 • Letter: H
Question
Hello, in this code I am basically trying to call the Addition method into my main in order to allow the user to input and see his results and so on. However I keep getting an error saying "result cannot be resolved into a variable". What does this error mean?
import java.util.*;
import java.util.Scanner;
import java.util.List;
public class ArithmeticOperators {
private static Scanner userChoice;
private static Scanner userChoiceTwo;
private static Scanner userChoiceThree;
private static Scanner userChoiceFour;
public static void main(String[]args){
System.out.println("Please choose to add, subjtract,");
System.out.println("multiply, or divide!");
int d = result;
}
public static int Addition(List<Integer> list){
int result;
userChoice = new Scanner(System.in);
LinkedList<Integer> aL = new LinkedList<Integer>();
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
result = a + b + c;
return result;
}
}
Explanation / Answer
As per your code,
import java.util.*;
import java.util.Scanner;
import java.util.List;
public class ArithmeticOperators {
private static Scanner userChoice;
private static Scanner userChoiceTwo;
private static Scanner userChoiceThree;
private static Scanner userChoiceFour;
public static void main(String[]args)
{
System.out.println("Please choose to add, subjtract,");
System.out.println("multiply, or divide!");
int d = result; //-- I think, you would get an error here as "result cannot be resolved into a variable."
}
public static int Addition(List<Integer> list)
{
int result; //-- here you have initialized a variable of 'result'.
userChoice = new Scanner(System.in);
LinkedList<Integer> aL = new LinkedList<Integer>();
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
result = a + b + c;
return result;
}
}
Reason:
The variable "result" is declared as local varibale of Addition method and it's not a part of the member of class variable.
"result" is not declared as varibale like private or public... and it is not passed as a parameter. So you can't access local variable from one method/function to another method/function.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.