Click here to Skip to main content
14,268,184 members
Rate this:
Please Sign up or sign in to vote.
See more:
expected output

“This summer semester is quite busy”

busy
is
quite
semester
summer
This

what I am getting

Enter your text, type done for exit:

this
summer
semester
is
quite
busy
done
************************
Sorted:

busy
is
quite
semester
summer
this

What I have tried:

package project;
import java.util.ArrayList;
import java.util.Scanner;
/**
 *
 * @author Rohith
 */
public class Project {

    /**
     * @param args the command line arguments
     */
     private ArrayList<String> strList;
 
    // Constructor
    public Project (ArrayList<String> input) {
        strList = input;
    }
     
    public void sort() {
        strList = mergeSort(strList);
    }
 
    public ArrayList<String> mergeSort(ArrayList<String> whole) {
        ArrayList<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();
        int center;
 
        if (whole.size() == 1) {    
            return whole;
        } else {
            center = whole.size()/2;
            // copy the left half of whole into the left.
            for (int i=0; i<center; i++) {
                    left.add(whole.get(i));
            }
 
            //copy the right half of whole into the new arraylist.
            for (int i=center; i<whole.size(); i++) {
                    right.add(whole.get(i));
            }
 
            // Sort the left and right halves of the arraylist.
            left  = mergeSort(left);
            right = mergeSort(right);
 
            // Merge the results back together.
            merge(left, right, whole);
        }
        return whole;
    }
 
    private void merge(ArrayList<String> left, ArrayList<String> right, ArrayList<String> whole) {
        int leftIndex = 0;
        int rightIndex = 0;
        int wholeIndex = 0;
 
        // As long as neither the left nor the right ArrayList has
        // been used up, keep taking the smaller of left.get(leftIndex)
        // or right.get(rightIndex) and adding it at both.get(bothIndex).
        while (leftIndex < left.size() && rightIndex < right.size()) {
            if ( (left.get(leftIndex).compareTo(right.get(rightIndex))) < 0) {
                whole.set(wholeIndex, left.get(leftIndex));
                leftIndex++;
            } else {
                whole.set(wholeIndex, right.get(rightIndex));
                rightIndex++;
            }
            wholeIndex++;
        }
 
        ArrayList<String> rest;
        int restIndex;
        if (leftIndex >= left.size()) {
            // The left ArrayList has been use up...
            rest = right;
            restIndex = rightIndex;
        } else {
            // The right ArrayList has been used up...
            rest = left;
            restIndex = leftIndex;
        }
 
        // Copy the rest of whichever ArrayList (left or right) was not used up.
        for (int i=restIndex; i<rest.size(); i++) {
            whole.set(wholeIndex, rest.get(i));
            wholeIndex++;
        }
    }
 
    public void show() {
        System.out.println("Sorted:");
        for (int i=0; i< strList.size();i++) {
            System.out.println(strList.get(i));
        }
    }
    
    public static void main(String[] args){
    ArrayList<String> input = new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
 
        System.out.println("Enter your text, type done for exit:");
        String strin = sc.nextLine();
 
 
        while(!strin.equals("done")) {
            input.add(strin);
            strin = sc.nextLine();
        }
        System.out.println("************************");
        Project test = new Project(input);
        test.sort();
        test.show();
    }
}
Posted
Comments
Patrice T 30-May-19 15:13pm
   
Explain what is the problem to you.
The "expected output" and "sorted" look very similar.
Richard MacCutchan 31-May-19 4:20am
   
You cannot get that expected output by using any sort method. The output you get when you run the program is the input data in sorted order. There is no way programmatically to guess what order the words should be in. Perhaps apart from assuming the word with a capital should be first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100