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

HELP IN JAVA: Write programs using the java built in Linked List class to do the

ID: 3882486 • Letter: H

Question

HELP IN JAVA: Write programs using the java built in Linked List class to do the following:

1. Collect 5 Strings from the keyboard and add them to a list.

2. Create a file of (unsorted) integers. Input them into a list so the list is sorted. Have at least 10 integers in your input file, but make your program work for any input file – meaning don’t use a for loop.

3. Use the file you created (above) as input. Create an array (of size 2) of Linked Lists (this is to help you with the homework question). Call this array A, and put all the odd numbers in the Linked List A[0], and all the even ones in A[1].

What I have so far:

public static void main(String[] args) throws FileNotFoundException {
Comparator<Integer> order = Integer::compare; //used for sorting the linkedlist
Scanner ks = new Scanner(System.in); //used for taking the input from keyboard
Scanner fs1 = new Scanner(new File("lab4.txt")); //used for taking the input from file
Scanner fs2 = new Scanner(new File("lab4.txt")); //used for taking the input from file

LinkedList<String> stringList = new LinkedList<String>();
LinkedList<Integer> intList = new LinkedList<Integer>();
LinkedList<Integer>[] A = new LinkedList[2];
A[0] = new LinkedList<Integer>();
A[1] = new LinkedList<Integer>();

//#1- Add 5 strings to the LinkedList
System.out.println("Enter 5 Strings: ");
for (int i = 0; i < 5; i++) {
stringList.add(ks.nextLine());
}

//#2- Add integers from file into LinkedList in sorted order
while (fs1.hasNext()) {
intList.add(fs1.nextInt());
}
intList.sort(order);

//#3- Add integers from file into array of linked lists
while (fs2.hasNext()) {
int a = fs2.nextInt();
if (a % 2 == 0) {
A[1].add(a);
} else {
A[0].add(a);
}

}

}
}

lab4.txt:

2 6 7 8 9 3 4 5 6 7

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
public class TeenageDetector {

      public static void main(String[] args) throws FileNotFoundException {
           // Comparator<Integer> order = Integer::compare; //used for sorting the linkedlist
            Scanner ks = new Scanner(System.in); //used for taking the input from keyboard
            Scanner fs1 = new Scanner(new File("lab4.txt")); //used for taking the input from file
            Scanner fs2 = new Scanner(new File("lab4.txt")); //used for taking the input from file

            LinkedList<String> stringList = new LinkedList<String>();
            LinkedList<Integer> intList = new LinkedList<Integer>();
            LinkedList<Integer>[] A = new LinkedList[2];
            A[0] = new LinkedList<Integer>();
            A[1] = new LinkedList<Integer>();

   //#1- Add 5 strings to the LinkedList
            System.out.println("Enter 5 Strings: ");
            for (int i = 0; i < 5; i++) {
                stringList.add(ks.nextLine());
            }

   //#2- Add integers from file into LinkedList in sorted order
            while (fs1.hasNext()) {
                intList.add(fs1.nextInt());
            }
            Collections.sort(intList);

   //#3- Add integers from file into array of linked lists
            while (fs2.hasNext()) {
                int a = fs2.nextInt();
                if (a % 2 == 0) {
                    A[1].add(a);
                } else {
                    A[0].add(a);
                }

            }

        }
   }


Thanks, let me know if there is any concern