Currently in my program I am trying to use and if else statement to choose either to add or subtract. I'm calling eitehr my addition method or my subtraction method in the statemenet. However i keep getting an error. "
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;
}
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,
Currently in my program I am trying to use and if else statement to choose either...
I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome to this Odd program!"); int userInput1 = input.nextInt(); } public...
Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q { public static LinkedList<String> dogs = new LinkedList<String> (); public static LinkedList<String> cats = new LinkedList<String> (); public static LinkedList<String> animals = new LinkedList<String> (); public static void enqueueCats(){ System.out.println("Enter name"); Scanner sc=new Scanner(System.in); String name = sc.next(); cats.addLast(name); animals.addLast(name); } ...
I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...
I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact { public static void main(String[] args) { Random Rand = new Random(); Scanner sc = new Scanner(System.in); System.out.println("welcome to the contact application"); System.out.println(); int die1; int die2; int Total; String choice = "y";...
IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...
I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); ...
Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count { public static void main(String args[]) { int n = getInt("Please enter an integer value greater than or equal to 0"); System.out.println("Should count down to 1"); countDown(n); System.out.println(); System.out.println("Should count up from 1"); countUp(n); } private static void countUp(int n) {...
Creating a calculator for my CS1 class and my code was able to run but it crashes near the end. As far as I know, the error means that it's retrieving "null" from the variable but I haven't seen that kind of error from this kind of situation. I would appreciate any help. I'm just starting up in Java so I apologize if this is a silly question. Thank you! ``` ----jGRASP exec: java CS1Calculator ------------ Welcome to the CS1...