-1

I have a database, where I need to make the user type two criterias they wanna sort the database by. If they for example want to sort it by name and height. It should then first sort it by name, and then height, but not change the first order it made using name.

I've tried to use an array, where I split the input. I use a switch case that accepts a string, where each case represents how they wanna sort it. If user for example type "1 2" where the first part is one sort, and the second is the second sort.

I hope it makes some sense :D

public void sortByTwoCriteria(){
    System.out.println("""
            1. Sort by name.
            2. Sort by height.
            3. Sort by power(s).
            4. Sort by weakness(ess).
            5. Sort by origin FROM earth.
            6. Sort by origin NOT from earth.
            """);

    String userInput = sc.nextLine().toLowerCase();

    ArrayList<Superhero> sortedByTwoCriteria = new ArrayList<>();


    String[] userInputs = new String[2];
    String userSplit = userInputs.split(" ");
    String command = userInputs[0];
    String userChoice = "";
    if (userInputs.length > 1) {
        System.out.println("You have only entered one parameter to sort by.");;
    }

    switch (command){
        case "1":
            //do something where it sorts by name.
            break;
        case "2":
            //do something where it sorts by height
            break;
        case "3":
            //do something where it sorts by power
            break;
        case "4":
            //do somehting where it sorts by weakness
            break;
        case "5":
            //do soemthing where it sorts by origin FROM earth (I use a boolean for it)
            break;
        case "6":
            //do something where it sorts by origin NOT from earth (I use a booleaen for it)
            break;




    }
}

Pretty lost tbh. I tried to copy some code from a older project, where it did something simmilar and tried to rewrite it to work for this.

  • https://stackoverflow.com/a/20093642/759049 – Pateman Nov 08 '22 at 14:24
  • 2
    `String[] userInputs = new String[2]; String userSplit = userInputs.split(" "); String command = userInputs[0];` there's something wrong here. First this doesn't compile because `split` is a member of `String` (while you're trying to split a string array) and returns an array of `String`s (not a `String`). Second, even if it did I suppose you meant to do something like `String[] userInputs = userInput.split(" ")` (notice that we are splitting `userInput`, the string you read with the scanner). Those variable names are confusing. – Federico klez Culloca Nov 08 '22 at 14:38
  • Not all of those look like sorting criteria. The last two look like grouping criteria, but if would probably be a good thing to post your `Superhero` class – g00se Nov 08 '22 at 18:10

1 Answers1

0

Just use 2 scanners to read values

int a1=sc.nextLine() //parse to int
int a2= sc.nextLine()

Add this to the array and use it in your switch case.

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Ajay J
  • 49
  • 8