Exception in thread \"main\" java.lang.Error: Unresolved compilation problem: at
ID: 3797072 • Letter: E
Question
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at ArithmeticOperators.main(ArithmeticOperators.java:19)"
I'm not sure what is causing this at all, below is my code.
import java.util.*;
public class ArithmeticOperators{
public LinkedList head;
private static Scanner userChoice;
public String toString() {
String result = "";
LinkedList current = head;
while(current.getFirst() != null){
current = (LinkedList) current.getFirst();
}
return "List: " + result;
}
public static void main(String[]args){
//mC stands for Math Choice
Scanner mC = new Scanner(System.in);
System.out.println("Enter your choice");
System.out.println("You can add or subtract");
String choice = mC.nextLine();
if(choice.equals("add")){
Addition(result);
}else if(choice.equals("minus")){
Subtraction(result2);}
System.out.println("Please choose FIVE numbers to add or subtract,");
}
public static int result;
public static int result2;
public static int Addition(int list){
userChoice = new Scanner(System.in);
LinkedList<Integer> aL = new LinkedList<Integer>();
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
int d = userChoice.nextInt();
int e = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
aL.add(d);
aL.add(e);
result = a + b + c + d + e;
System.out.println("The elements of your list are:" + aL.toString());
System.out.println("The result of your numbers are: " + result);
return result;
}
public static int Subtraction(int list){
userChoice = new Scanner(System.in);
LinkedList<Integer> aL = new LinkedList<Integer>();
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
int d = userChoice.nextInt();
int e = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
aL.add(d);
aL.add(e);
result = a - b - c - d - e;
System.out.println("The elements of your list are:" + aL.toString());
System.out.println("The result of your numbers are: " + result);
return result2;
}
Explanation / Answer
Hi buddy, I've identified one mistake in your code. You haven't given closing braces at the end of the java class. Here is the corrected code and the output.
import java.util.*;
public class ArithmeticOperators{
public LinkedList head;
private static Scanner userChoice;
public String toString() {
String result = "";
LinkedList current = head;
while(current.getFirst() != null){
current = (LinkedList) current.getFirst();
}
return "List: " + result;
}
public static void main(String[]args){
//mC stands for Math Choice
Scanner mC = new Scanner(System.in);
System.out.println("Enter your choice");
System.out.println("You can add or subtract");
String choice = mC.nextLine();
if(choice.equals("add")){
Addition(result);
}else if(choice.equals("minus")){
Subtraction(result2);}
System.out.println("Please choose FIVE numbers to add or subtract,");
}
public static int result;
public static int result2;
public static int Addition(int list){
userChoice = new Scanner(System.in);
LinkedList<Integer> aL = new LinkedList<Integer>();
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
int d = userChoice.nextInt();
int e = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
aL.add(d);
aL.add(e);
result = a + b + c + d + e;
System.out.println("The elements of your list are:" + aL.toString());
System.out.println("The result of your numbers are: " + result);
return result;
}
public static int Subtraction(int list){
userChoice = new Scanner(System.in);
LinkedList<Integer> aL = new LinkedList<Integer>();
int a = userChoice.nextInt();
int b = userChoice.nextInt();
int c = userChoice.nextInt();
int d = userChoice.nextInt();
int e = userChoice.nextInt();
aL.add(a);
aL.add(b);
aL.add(c);
aL.add(d);
aL.add(e);
result = a - b - c - d - e;
System.out.println("The elements of your list are:" + aL.toString());
System.out.println("The result of your numbers are: " + result);
return result2;
}
}
OUTPUT:
D:>java ArithmeticOperators
Enter your choice
You can add or subtract
add
3
5
6
8
6
The elements of your list are:[3, 5, 6, 8, 6]
The result of your numbers are: 28
Please choose FIVE numbers to add or subtract,
D:>java ArithmeticOperators
Enter your choice
You can add or subtract
minus
5
1
2
4
3
The elements of your list are:[5, 1, 2, 4, 3]
The result of your numbers are: -5
Please choose FIVE numbers to add or subtract,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.