Hi guys, I'm new here and I have a activity in my java class.
my instructor want to sort all the given names after they added to the list. can you guys help me with it? thanks

here's my code:

import java.util.*;
public class Test{
    public static void main (String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("IT Students");
        LinkedList list1 = new LinkedList();

        int y;
        do{
            System.out.println("(1) Add");
            System.out.println("(2) View");
            System.out.println("(3) Edit");
            System.out.println("(4) Delete");
            System.out.println("(5) Exit");
            System.out.println("Enter number of choice: ");

            String name;
            y = sc.nextInt();
            if (y==1){
                System.out.println("Enter a student name: ");
                    String name1 = sc.next();
                    list1.add(new String(name1));
            }

            else if (y==2){
                System.out.print(list1);
                System.out.println(" ");
            }
            else if (y==3){
                System.out.println("Enter the name to edit: ");
                String edit = sc.next();
                list1.remove(new String(edit));
                System.out.println(" ");
                System.out.print("Enter a new name: ");
                String edit2 = sc.next();
                list1.add(new String(edit2));
            }
            else if (y==4){
                System.out.println("Enter the name you want to delete: ");
                String del = sc.next();
                list1.remove(new String(del));
                System.out.println(" ");
            }
            else if (y==5){
                System.out.print("Exit");
            }
        }while (y!=5);
    }
}

Recommended Answers

All 3 Replies

I think you can use Collections.sort(LinkedListObject)

thank you for the link sir Hiroshe and Slavi i wll try that. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.