Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m suppose to create a Java code that can read any 5 integer numbers using Lin

ID: 3545238 • Letter: I

Question

I'm suppose to create a Java code that can read any 5 integer numbers using LinkedList.

I know it has double in the code, but can someone help me change it where it only reads integers and it compiles correctly. Thank you!! My professor corrected me saying she wants the numbers in "integers' and not "doubles".


import java.util.List;

import java.util.Scanner;


public class LinkedList {


public static void main(String[] args) {

double sum = 0;

List<Double> linkedlist = new LinkedList<Double>();

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the first number");

linkedlist.add(scanner.nextDouble());

System.out.println("Enter the second number");

linkedlist.add(scanner.nextDouble());

System.out.println("Enter the third number");

linkedlist.add(scanner.nextDouble());

System.out.println("Enter the first number");

linkedlist.add(scanner.nextDouble());

System.out.println("Enter the first number");

linkedlist.add(scanner.nextDouble());

for (Double d : linkedlist) {

sum += d;

}

System.out.println("The sum of all the numbers is :" + sum);

}

}

Explanation / Answer

import java.util.*;
public class LinkedList_local {
public static void main(String[] args) {
int sum = 0;
LinkedList linkedlist = new LinkedList();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number");
linkedlist.add(scanner.nextInt());
System.out.println("Enter the second number");
linkedlist.add(scanner.nextInt());
System.out.println("Enter the third number");
linkedlist.add(scanner.nextInt());
System.out.println("Enter the first number");
linkedlist.add(scanner.nextInt());
System.out.println("Enter the first number");
linkedlist.add(scanner.nextInt());
for (Object d : linkedlist) {
sum += (Integer)d;
}
System.out.println("The sum of all the numbers is :" + sum);

}

}