Question
I am hitting a bit of a wall getting my results to display correctly. Any help would be greatly appreciated. The results should look like
I am hitting a bit of a wall getting my results to display correctly. Any help would be greatly appreciated.
The results should look like this -
But mine are turning out like this -
This is the code I am working with -
package u5a1_sortlinkedlist;
import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; /** * * @author Andrea */ public class U5A1_SortLinkedList {
public static void main(String args[])
{
// Creating object of class linked list
LinkedList list1 = new LinkedList();//store set 1 course code LinkedList list2 = new LinkedList();//store set 2 course code LinkedList list3 = new LinkedList();//store set 3 course code
System.out.println("Enter 3 collections of course codes. One collection per line: ");
// predefined standard input object
Scanner sc = new Scanner(System.in); String set1=sc.nextLine();//input one line at time for set String set2=sc.nextLine(); String set3=sc.nextLine();
//spliting of set in to String array
String [] arrOfStr1 = set1.split(" ", -2); String [] arrOfStr2 = set2.split(" ", -2); String [] arrOfStr3 = set3.split(" ", -2);
//traverse string array and push in to linklist list1.addAll(Arrays.asList(arrOfStr1)); //traverse string array and push in to linklist list2.addAll(Arrays.asList(arrOfStr2)); //traverse string array and push in to linklist boolean addAll = list3.addAll(Arrays.asList(arrOfStr3)); //traverse string array and push in to linklist
//sort the linklist
Collections.sort(list1); Collections.sort(list2); Collections.sort(list3);
System.out.println(list1.size()); System.out.println("Size: 4 Sorted: " + list1);
//iterate over linklist to print the Course code
for(int num=0; num
{
System.out.println(list1.get(num)); System.out.println(); }
System.out.println(list2.size()); System.out.println("Size: 2 Sorted:" + list2);
//iterate over linklist to print the Course code for(int num=0; num
{
System.out.println(list2.get(num)); System.out.println(); } System.out.println(list3.size()); //iterate over linklist to print the Course code System.out.println("Size: 1 Sorted:" + list3); for(int num=0; num
{
System.out.println(list3.get(num)); System.out.println(); }
}
}
run: Teacher's Copy Enter 3 collections of course codes one collection per line: IT4789 IT4782 IT4786 IT4784 Size: 4 Sorted: IT4782 IT4784 IT4786 IT4789 IT3345 IT2230 Size: 2 Sorted: IT2230 IT3345 IT2230 Size: 1 Sorted: IT2230 UTLDSUCBSSrUL (total tine: 1 inute 20 sconda)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started