0

I am trying to sort my arrylist based on the grade level. But I dont know why it prints out nothing, and I check the arraylist, showing thats empty. Plz help me. I am a current AP CS student. The "names" is the list I import from the library,dont worry about it.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JOptionPane;

public class lab {

    public static ArrayList<String> method2(ArrayList<String> random) {
        ArrayList<String> lastfirst = new ArrayList<String>();
        for (int a = 0; a < random.size(); a = a + 2) {
            String str = random.get(a);
            int space = str.indexOf(" ");
            String last = str.substring(space + 1);
            String first = str.substring(0, space);
            String combo = last + " " + first;
            String str2 = random.get(a + 1);
            String namegrade = combo + "," + str2 + ",";
            lastfirst.add(namegrade);
        }
        ArrayList<String> prek = new ArrayList<String>();
        ArrayList<String> jrprek = new ArrayList<String>();
        ArrayList<String> k = new ArrayList<String>();
        ArrayList<String> one = new ArrayList<String>();
        ArrayList<String> two = new ArrayList<String>();
        ArrayList<String> three = new ArrayList<String>();
        ArrayList<String> four = new ArrayList<String>();
        ArrayList<String> five = new ArrayList<String>();
        ArrayList<String> six = new ArrayList<String>();
        ArrayList<String> seven = new ArrayList<String>();
        ArrayList<String> eight = new ArrayList<String>();
        ArrayList<String> nine = new ArrayList<String>();
        ArrayList<String> ten = new ArrayList<String>();
        ArrayList<String> eleven = new ArrayList<String>();
        ArrayList<String> twelve = new ArrayList<String>();

        for (String str : lastfirst) {
            int comma = str.indexOf(",");
            if (str.substring(comma + 1) == "**Pre-K") {
                prek.add(str);
            }
            if (str.substring(comma + 1) == "**Jr Pre-K") {
                jrprek.add(str);
            }
            if (str.substring(comma + 1) == "*K") {
                k.add(str);
            }
            if (str.substring(comma + 1) == "1") {
                one.add(str);
            }
            if (str.substring(comma + 1) == "2") {
                two.add(str);
            }
            if (str.substring(comma + 1) == "3") {
                three.add(str);
            }
            if (str.substring(comma + 1) == "4") {
                four.add(str);
            }
            if (str.substring(comma + 1) == "5") {
                five.add(str);
            }
            if (str.substring(comma + 1) == "6") {
                six.add(str);
            }
            if (str.substring(comma + 1) == "7") {
                seven.add(str);
            }
            if (str.substring(comma + 1) == "8") {
                eight.add(str);
            }
            if (str.substring(comma + 1) == "9") {
                nine.add(str);
            }
            if (str.substring(comma + 1) == "10") {
                ten.add(str);
            }
            if (str.substring(comma + 1) == "11") {
                eleven.add(str);
            }
            if (str.substring(comma + 1) == "12") {
                twelve.add(str);
                return twelve;
            }

        }

        for (String str : twelve) {
            System.out.print(str + " ");
            System.out.println();
        }
        return twelve;

    }

    public static void main(String[] args) {

        method1(names);
        if (!(method2(names).isEmpty())) {
            JOptionPane.showMessageDialog(null, "List isn't empty");
        } else {
            JOptionPane.showMessageDialog(null, "List is empty!");
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Just for the imports to look cleaner, just use `import java.util.*;`, etc. It looks cleaner, takes less space, and you won't have to change any of the other code. – MLavrentyev Apr 24 '15 at 01:10
  • 2
    `str.substring(comma + 1) == "12"` is not how `String`s are compared in Java – MadProgrammer Apr 24 '15 at 01:12
  • Welcome to Stack Overflow! You should never compare Strings with `string1 == string2`; always use `string1.equals(string2)`. Essentially, `==` checks if the two strings are the exact same object (`new String("1")` and `new String("1")` are not), but `.equals` checks if the internal character arrays contain the same values. For more information, see [How do I compare strings in Java?](http://stackoverflow.com/q/513832/2846923). – The Guy with The Hat Apr 24 '15 at 01:21

0 Answers0