I need to sort these strings in order and I have changed the code of my program which use to sort integers and now I need it to sort strings and I cannot get it to work, any help.....

import java.util.Scanner;

public class Strings {


// --------------------------------------------
// Reads in an array of strings, sorts them,
// then prints them in sorted order.
// --------------------------------------------
public static void main(String[] args) {

    stringList = new String;
    String size;
            Comparable temp;

    Scanner scan = new Scanner(System.in);

    System.out.print("\nHow many strings do you want to sort? ");
    size = scan.nextInt();
    StringList = new String[size];

    System.out.println("\nEnter the string(s)...");
    for (String i = 0; i < size; i++)
        StringList[i] = scan.nextInt();
    Sorting.insertionSort(StringList);

    System.out.println("\nYour strings in sorted order...");
    for (String i = 0; i < size; i++)
        System.out.print(StringList[i] + " ");

        System.out.println();
    }
}

Dani AI

Generated

The posted program has several clear syntax and logic problems that prevent compilation and proper string sorting: the array-size is declared with the wrong type, the array creation syntax is incorrect, loop counters are declared as String instead of int, input is read with nextInt() instead of a method that returns text, and the call to Sorting.insertionSort(...) expects a sorting method/class that either must exist or be replaced. Fixing those issues (correct types, consistent names, proper input calls) is enough to get a working string sorter.

A compact, correct example that reads N lines and performs an insertion sort (case-insensitive) is shown below.

import java.util.Scanner;

public class SortStrings {
    public static void insertionSort(String[] a) {
        for (int i = 1; i < a.length; i++) {
            String key = a[i];
            int j = i - 1;
            while (j >= 0 && a[j].compareToIgnoreCase(key) > 0) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = key;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("How many strings? ");
        int n = scan.nextInt();
        scan.nextLine(); // consume newline
        String[] list = new String[n];
        for (int i = 0; i < n; i++) list[i] = scan.nextLine();
        insertionSort(list);
        for (String s : list) System.out.println(s);
    }
}

Notes and quick troubleshooting: use scan.nextLine() (or scan.next() if no spaces are expected) to read strings after nextInt() and remember to consume the leftover newline; Java identifiers are case-sensitive (StringList vs stringList are different); choose compareToIgnoreCase for case-insensitive lexicographic order or compareTo for case-sensitive. As pointed out, the built-in java.util.Arrays.sort(...) is the simplest alternative for production use; as noted, if a separate Sorting class is being used, confirm its insertionSort signature accepts String[] or Comparable[] before calling it.

Recommended Answers

All 2 Replies

If you want to sort an array of Strings, you can do the following:

// An example array of Strings
String[] myArray = new String[] {"orange", "banana", "apple", "melon", "grape"}
java.util.Arrays.sort(myArray);

I could see some compilation problems here at lines 12 and 20.
There is also another missing piece:

Sorting.insertionSort(StringList);

Can you give the details of this class Sorting which is having the method insertionSort(String[] arrayOfStrs)

Using this information we can find what sorting algorithm is there and fix the bugs if any

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.