0

Okay so here's what I have so far:

Scanner key = new Scanner (System.in);
String[] main = new String[10];

for(int k = 0; k< main.length; k++)
{
   System.out.println("Enter a lower case letter! ");
   main[k] = key.nextLine();
}
for(int z = 1; z < 11; z++)
{
    char[] array = main[z-1].toCharArray();
    System.out.println(array);
}        
for(char c : array)
{
   System.out.println(c);
}

However, I have not found a way to print it, I think the char array is set up correctly, but I have little knowledge of how char arrays work. Also, it is worth noting that I cannot use Arrays.sort; however, I can use String.compareTo. I am trying to get a string array like a,j,d,f,h,y,d,a,d,g to print it out in alphabetical order without using Arrays.sort.

Brian Matthews
  • 8,506
  • 7
  • 46
  • 68
  • The code you posted can't compile/ You're trying to access `array` from the outside of the block where it's defined. What are you trying to achieve? Use an example. Let's say you have an array containing "hello", "world" and 8 other strings. Then what? – JB Nizet Nov 15 '16 at 20:54
  • is your question to sort the array, or print it? you can find how to print an array here http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array. – baseballlover723 Nov 15 '16 at 20:54
  • If you want to sort the array without using a built in, I would recommend some of the simpler sorting algorithms. check out bubble sort for example. it is not an efficient algorithm, but it is a straight forward one. – chatton Nov 15 '16 at 20:57

1 Answers1

3

You can use java.util.Arrays sort() method.

//public static void sort(char[] a)

char[] a = {'c','a','b'};
Arrays.sort(a);

You can convert String to char array and sort the char array and create a new String out of it.

If you want to sort array of Strings(or in general for any Objects which implement Comparable interface), use

public static void sort(Object[] a)
prem kumar
  • 5,641
  • 3
  • 24
  • 36