0

Could anybody tell me how to list some data in an arrayList according to the integer value that each component of the ArrayList has? This is my main class

import java.util.Scanner;
import java.io.*;
import java.util.Collections;
import java.util.ArrayList;
public class StudentDriver {

public static void main(String[] args) throws IOException {

    Scanner scan, urlScan, fileScan;
    String url, file;
    int count = 0;
    scan = new Scanner(System.in);
    System.out.println("Enter the name of the file");
    fileScan = new Scanner(new File("Data.csv"));
    ArrayList<Student> studentList = new ArrayList<Student>();
        while(fileScan.hasNext()){
            url = fileScan.nextLine();
            urlScan = new Scanner(url);
            urlScan.useDelimiter(",");
            count++;
            while(urlScan.hasNext()){
                String name = urlScan.next();
                String last = urlScan.next();
                int score = urlScan.nextInt();
                Student e = new Student(name,last, score);
                studentList.add(e);

            }   
        }
        System.out.println("The file has data for" +count+ "instances");
        int option;
        do{

        System.out.println("********");
        System.out.println("Options:");
        System.out.println("********\n1. List \n2. Add Student \n3.Delete Student \n4. Exit \n******** ");
        System.out.print("Select option: ");
        option = scan.nextInt();

        if(option == 1){    
        int index = 0;
        while(index<studentList.size()){
            System.out.println(studentList.get(index));
            index++;
        }
        }
        else if(option == 2){
            System.out.print("Enter the name of the student: ");
            String newName = scan.next();
            System.out.print("Enter the last name of the student: ");
            String newLastName = scan.next();
            System.out.print("Enter the exam score of the student: ");
            int newScore = scan.nextInt();
            Student b = new Student(newName, newLastName, newScore);
            studentList.add(b);}
        else if(option == 3){
            System.out.print("Enter the name of the student to remove: ");
            String remove = scan.next();
            System.out.print("Enter the last name of the student: ");
            String remove1 = scan.next();
            int location = studentList.indexOf(remove);
            location = studentList.indexOf(remove1);

            studentList.remove(location);
            }   

        }while(option!=4 && option <4);
    }//main
}//class

And this is the other class

public class Student implements Comparable<Student>{
    String firstName, lastName;
    int score;

public Student(String firstName, String lastName, int score){
    this.firstName = firstName;
    this.lastName = lastName;
    this.score = score;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getScore() {
    return score;
}

public void setScore(int score) {
    this.score = score;
}
public String toString(){
    return firstName + " " + lastName + ", exam score is "+ score;
}

@Override
public int compareTo(Student c) {

    return score-c.getScore();
}

}

As you can see, up to now I have created the class where my compare method is but I have difficulties on using it. Also I have had difficulties on deleting one of the Array List parts by just writing the name and last name of the student. If somebody would help me, I would be very thankful.

8
  • 1
    Think about a Comparator. Commented May 1, 2015 at 15:31
  • If you need any more information about what I want to do just tell me. Commented May 1, 2015 at 15:32
  • Sounds like you want a TreeSet<Student>, not a List. Commented May 1, 2015 at 15:33
  • I am required to use an ArrayList(from my instructor). And I don't know how the TreeSet works btw Commented May 1, 2015 at 15:39
  • 1
    I don't need more information; more effort on your part is what's required. Commented May 1, 2015 at 15:42

4 Answers 4

0

well you can change your compareTo method as

public int compareTo(Student another)
{
    if (this.score > another.score)
        return -1;
    if (this.score < another.score) 
        return 1;
    else
        return 0;
}

this should show it as decreasing order you can change the operator

than use whereever you want to sort it

Collections.sort(studentList)

Also if you don't want to use Collections.sort() method I can show you how you can write it with for loop under add option

Student newStd = new Student(name, last, score);


                for(int i=0;studentList.size()>i;i++)
                {
                    int size = studentList.size();
                    if(newStd.compareToCustom(studentList.get(i))>0)
                    {
                        studentList.add(i, newStd);
                        break;
                    }
                    else if(newStd.compareToCustom(studentList.get(size-1))<0)
                    {
                        studentList.add(studentList.size(), newStd);
                        break;
                    }
                    else if(newStd.compareToCustom(studentList.get(i))==0)
                    {
                        studentList.add(i++, newStd);
                        break;
                    }
                }

for the remove part you can use

else if ( option == 3)
            {
                System.out.print("Enter the first name of student will be deleted: ");
                String removeName = scan.next();
                System.out.print("Enter the last name of student will be deleted: ");
                String removeLastName = scan.next();

                for ( int i = 0; i < studentList.size(); i++)
                {
                    Student deleted = studentList.get(i);

                    if ( deleted.getFirstName().toLowerCase().equals(removeName.toLowerCase()) && deleted.getLastName().toLowerCase().equals(removeLastName.toLowerCase()))
                    {
                        studentList.remove(i);
                        System.out.println("The student has been deleted.");
                        break;
                    }
                    else
                    {
                        System.out.println("This student is not found");
                        break;
                    }

                }

            }
Sign up to request clarification or add additional context in comments.

Comments

0

Basically what you want is an ordered collection. As @duffymo has stated, think about a creating a custom Comparator using your score.

There is plenty of info here

Comments

0

In terms of deleting students from the list. The studentList is a list containing Student objects. This means that the follow code:

System.out.print("Enter the name of the student to remove: ");
String remove = scan.next();
System.out.print("Enter the last name of the student: ");
String remove1 = scan.next();
int location = studentList.indexOf(remove);

Tries to find the index of a Student given the first name. This will return -1 as you're searching for a String and not a Student object. Instead you have to iterate through your studentList and compare the first and last name of each Student element with the values of remove and remove1.

for(Student student : studentList) {
    if(student.getFirstName.equals(remove) && student.getLastName.equals(remove1)) {
        // remove the student.
    }
}

Also you could consider giving each Student an ID as an unique identifier.

2 Comments

You won't be able to remove a student while iterating over studentList (concurrent modification exception).
Then save the index or something. The idea holds.
0

try this to sort studentList

  Collections.sort(studentList, new Comparator<Student>() 
    {
        @Override
        public int compare(Student  x, Student  y)
        {
            if(x.score >= y.score)
                return 1;
            else 
                return -1;

        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.