Question

e eclipse ava Arithmeticop java Pse erators/src/Arithmeticoperators File Edit Source Refactor Navigate Search Project Run Window Help la Package Exp Ari hrnelicope Two java project eticOperators java int e userChoice next 71 Int AminoAcids 72 73 add a b Arithrreticoperators L. add(b); 74 L. add(c 75 (Jelaulu package 76 aL add d etioope 77 al. add(e); result a b c d e; Linked Stack 78 System out rintln (The elements of your list are: aL toString 79 a JRF System Library system. out println (The result of your numbers are: result 80 ArithmetiroperatorsTv 81 return result 82 th detault package 83 ArithmeticOpel 85 Link java RE System Library 87 88 89 91. di Problems Javadoc Declaration Console 3s <terminated> ArithmeticOperators Clava A ication] CAP m Fil esJavaMirc1.8.0 121 binjavaw.exe (Feb 22, 2017, 9:1 12 PM) Exception in thread main java lang. Error: Unresolved compilation problem at Arithmeticoperators main (Arithmetic erators ava: 19 Ask me anything Quick Access 9:21 P 2/22/2017Currently 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;

  


}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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,

Add a comment
Know the answer?
Add Answer to:
Currently in my program I am trying to use and if else statement to choose either...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to make a linked list queue and I am trying to use the...

    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 am trying to run a couple of functions in my main and It won't output...

    I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code. package a3; import java.util.Scanner; public class LoopsAndImages { public static void main(String[] args){ String test = "A rabbit has a carrot"; Boolean numbers = hasMoreEvenThanOdd("12344"); System.out.println(test); System.out.println(hideLetterA(test)+" This is the...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    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...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    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 was wanting to know how would I call my add class through my if statement....

    I was wanting to know how would I call my add class through my if statement. Here is my class: import java.util.Scanner; public class Main {    public static void main(String[] args)    {        int num1 = 0;        int num2 = 0;        int answer = 0;        int userInput = 0;        Scanner sc = new Scanner(System.in);                      System.out.println("Hello, this is a program where you can choose...

  • I am currently doing homework for my java class and i am getting an error in...

    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";...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as...

    Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as a new list. 1) Implement a method: mergeTwoLists(…){…} 2) Use the LinkedList library in Java 3) Test your method. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 ````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` import java.util.*; public class Inclass1 { public static void main(String[] args){ //Question 5.2 LinkedList l1 = new LinkedList<>(); LinkedList l2 = new LinkedList<>(); l1.add(1); l1.add(2); l1.add(10); l2.add(1); l2.add(3); l2.add(4); LinkedList l3 = mergeTwoLists(l1, l2); System.out.println("Question \n******************************"); for(int i...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    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...

  • I need to add a method high school student, I couldn't connect high school student to...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT