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.

3
  • 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? Commented Nov 15, 2016 at 20:54
  • is your question to sort the array, or print it? you can find how to print an array here stackoverflow.com/questions/409784/…. Commented Nov 15, 2016 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. Commented Nov 15, 2016 at 20:57

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

6 Comments

I cannot use any imported utilities, sorry
but it belongs to java.utils package not external dependency
@premkumar You imported Scanner.
@bradimus how? only java.util.Arrays will be imported .
Sorry, comment was meant for @TheFaceHugger
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.