Tenebris Mortem
Tenebris Mortem

Reputation: 93

Function is just reversing the order of the input instead of sorting it in descending order

I need to make a program that will take string inputs from user and store it in an array. I will then need to make a function that first: sorts each String {character by character} in descending order and second: will sort all String input in descending order {Strings}.

package com.company;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static String sortString(String str)
    {
        
        char[] chArr = str.toCharArray();
        String SortString = "";

        // For sorting each individual strings character by character
        for (int i = 0; i< chArr.length; i++)
        {
            for (int j = 0; j < chArr.length; j++)
            {
                if(chArr[i] > chArr[j])
                {
                    char temp = chArr[i];
                    chArr[i] = chArr[j];
                    chArr[j] = temp;
                }
            }
        }

        //converting all of the character into a single string
        for (int k = 0; k<chArr.length;k++)
        {
            SortString = SortString + chArr[k];
        }

        //Assigning the current String Sortstring to an array
        String[] OldArray = new String[5];
        for (int counter = 0; counter<5; counter++)
        {
            OldArray[counter] = SortString;
        }

        //sorting all of the strings in descending order
        for (int i = 0; i< OldArray.length;i++)
        {
            for (int j = i+1; j< OldArray.length;j++)
            {
                if(OldArray[i].compareTo(OldArray[j]) > 0)
                {
                    String temp = OldArray[i];
                    OldArray[i] = OldArray[j];
                    OldArray[j] = temp;
                }
            }
        }

        return OldArray[0];
    }

    public static void main(String[] args)
    {
        Scanner UserInput = new Scanner (System.in);

        String[] names = new String[5];

         // will take a String user input from a user and store it in an arra
        for (int counter = 0; counter<5; counter++)
        {
            do
            {
                System.out.print("Input String #" + (counter+1) + ": ") ;
                names[counter] = UserInput.next().toLowerCase();
            }while(names[counter].length() > 25);
        }

        //will print the assorted array
        for(int i = 4; i >= 0; i--)
        {
            System.out.println((sortString(names[i])));
        }


    }

}

Input:

Input String #1: Stackoverflow
Input String #2: Java
Input String #3: ZZrot
Input String #4: coding
Input String #5: sorting

Output

tsronig
onigdc
zztro
vjaa
wvtsroolkfeca

Expected Output:

zztro
wvtsroolkfeca
vjaa
tsronig
onigdc

Sorry for the question I honestly don't know what to do

Upvotes: 0

Views: 48

Answers (2)

Dinesh Dontha
Dinesh Dontha

Reputation: 579

Just made some changes to your code. sortString() was working fine.

Made only changes to main() method:

Got expected output, Try this:

public static void main(String[] args)
{
    Scanner UserInput = new Scanner (System.in);
    String[] names = new String[5];
    // will take a String user input from a user and store it in an arra
    for (int counter = 0; counter<5; counter++)
    {
        do
        {
            System.out.print("Input String #" + (counter+1) + ": ") ;
            names[counter] = UserInput.next().toLowerCase();
        }while(names[counter].length() > 25);
    }

    //will print the assorted array
    String[] namesReversed = new String[names.length];
    for(int i=0;i<names.length;i++){
        namesReversed[i]=sortString(names[i]);
    }
    Arrays.sort(namesReversed, String::compareToIgnoreCase);
    for(int i = namesReversed.length-1; i>=0; i--)
    {
        System.out.println(namesReversed[i]);
    }
}

Upvotes: 1

Joni
Joni

Reputation: 111269

You're very close to the solution.

It's impossible to sort the array of strings in sortString because it only has access to the one string you pass in. Move the array sorting code to a separate method, and then you can call it while passing it the entire array:

static String sortString(String str) {

    char[] chArr = str.toCharArray();
    String SortString = "";

    // For sorting each individual strings character by character
    for (int i = 0; i < chArr.length; i++) {
        for (int j = 0; j < chArr.length; j++) {
            if (chArr[i] > chArr[j]) {
                char temp = chArr[i];
                chArr[i] = chArr[j];
                chArr[j] = temp;
            }
        }
    }

    //converting all of the character into a single string
    for (int k = 0; k < chArr.length; k++) {
        SortString = SortString + chArr[k];
    }

    return SortString;
}

static void sortArray(String[] OldArray) {

    //sorting all of the strings in descending order
    for (int i = 0; i< OldArray.length;i++)
    {
        for (int j = i+1; j< OldArray.length;j++)
        {
            if(OldArray[i].compareTo(OldArray[j]) > 0)
            {
                String temp = OldArray[i];
                OldArray[i] = OldArray[j];
                OldArray[j] = temp;
            }
        }
    }
}

The main method needs a small change too: the characters in the strings have to be sorted before you sort the array. Here, the characters are sorted while reading the input, and then the array is sorted with one call to sortArray:

public static void main(String[] args)
{
    Scanner UserInput = new Scanner (System.in);

    String[] names = new String[5];

    // will take a String user input from a user and store it in an arra
    for (int counter = 0; counter<5; counter++)
    {
        do
        {
            System.out.print("Input String #" + (counter+1) + ": ") ;
            names[counter] = sortString(UserInput.next().toLowerCase());
        }while(names[counter].length() > 25);
    }

    sortArray(names);

    //will print the assorted array
    for(int i = 4; i >= 0; i--)
    {
        System.out.println(names[i]);
    }
}

Upvotes: 1

Related Questions