DrPuff
DrPuff

Reputation: 1

Using SplitString to separate input sentence and to sort it in descending order based on its letters

I got a base for how to do this but It only does two words and not the whole 5 that i put in. (Testing this as an input: "This is a test message"). We are allowed to use String str="This is a test message"; String [ ] tokens= str.split(" "); Please help! (Ignore the words int the parenthesis, I need more words to post this)

public static void main(String[] args){
    // TODO Auto-generated method stub

    Scanner console = new Scanner(System.in);

    System.out.println("Enter a String:");

    String enter = console.nextLine();
    String[] toke = enter.split(" ");

    for(String retrieve: toke)
    {
        System.out.println(retrieve);
    }
    String[] group = new String[toke.length];
    for(int a = 0; a < group.length; a++)
    {
        group[a] =" ";
    }
    for(int a = 0; a < toke.length; a++)
    {
        for(int b =0; b< group.length; b++)
        {
            if(toke[a].length() > group[b].length())
            {
                for(int c = 0; c < b; c++)
                {
                    if(b + a < group.length)
                    {
                        group[c + b] = group[c + b + 1];
                        for(int d = 0; d < c; d++)
                        {
                            if(b + c <= group.length)
                            {
                                group[d + c] = group[d + c + 1 ];
                            }
                        }
                        group[c + b + 1] = toke[a] + group[a];
                    }
                }
                group[b] = toke[a];
                break;
            }
        }

    }
    System.out.println("The Sorted Tokens in Descending Order:");
    for(String retrieve: group)
    {
        System.out.println(retrieve);
    }


}

}

Upvotes: 0

Views: 730

Answers (2)

Boris
Boris

Reputation: 805

Can't you just use Arrays.sort();

Here's an example,

    Scanner scan = new Scanner(System.in);
    String[] line = scan.nextLine().split(" ");
    Arrays.sort(line); //if you want from smallest -> biggest
    Collections.reverse(Arrays.asList(line)); // if you want biggest -> smallest, add this.
    for(String s : line){
        System.out.print(s + " ");
    }

Upvotes: 1

Tachyon
Tachyon

Reputation: 452

May be this could help:

public static void main(String[] args) {
    // TODO code application logic here

    Scanner console = new Scanner(System.in);

    System.out.println("Enter a String:");

    String enter = console.nextLine();
    String[] toke = enter.split(" ");

    for(String retrieve: toke){
        System.out.println(retrieve);
    }

    String temp;
    for(int i=0; i < toke.length; i++){
        for(int j=i+1 ; j<toke.length; j++){
            if(toke[i].compareTo(toke[j])>0){
                temp=toke[i];
                toke[i]=toke[j];
                toke[j]=temp;
            }
        }
    }

    System.out.println("The Sorted Tokens in Descending Order: ");
    for(String retrieve: toke){
        System.out.println(retrieve);
    }
}

Upvotes: 0

Related Questions