0

I want to have a user input a random string of letters, put those in an array, then sort them alphabetically. Problem I have is putting the input into an array. What I have is:

import java.util.Scanner;
public class ArraySort {
public static void main(String[] args)
{
    System.out.println("Enter letters");
    Scanner kb = new Scanner(System.in);
    String input = kb.nextLine();
    int stringLength = input.length();
    String[] stringArray = new String[stringLength];

    for (int i = 0; i < stringLength; i++)
    {
        stringArray[i] = input;     
    }

    System.out.println(stringArray);

}

}

This gives me [Ljava.lang.String;@55f96302 when I print.

AndoTwo
  • 39
  • 1
  • 8

3 Answers3

3

You have two problems, you're not printing the Array correctly, and you're storing the entire input in each cell of the array. Try:

    for (int i = 0; i < stringLength; i++)
    {
        stringArray[i] = input.charAt(i)+"";  
        System.out.println(stringArray[i]);   
    }
Ryan
  • 2,058
  • 1
  • 15
  • 29
1

You are making 2 major mistakes:

1) You are assigning each string the whole input stringArray[i] = input;

2) You have to iterate over each element of your string array. In Java8 this could be done easily with Arrays.stream().

A corrected Version of your code is:

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        System.out.println("Enter letters");
        Scanner kb = new Scanner(System.in);
        String input = kb.nextLine();
        int stringLength = input.length();
        String[] stringArray = new String[stringLength];

        for (int i = 0; i < stringLength; i++)
        {
            stringArray[i] = Character.toString(input.charAt(i));
        }

        Arrays.stream(stringArray).forEach(System.out::print);

    }
}

Btw. String[] stringArray=input.split(""); would be much shorter.

Additional: If you want sorted output:

stringArray=Arrays.stream(stringArray).sorted().toArray(String[]::new);
Arrays.stream(stringArray).forEach(System.out::print);

And you are done.

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        System.out.println("Enter letters");
        Scanner kb = new Scanner(System.in);
        String input = kb.nextLine();
        String[] stringArray=input.split("");
        stringArray=Arrays.stream(stringArray).sorted().toArray(String[]::new);

        Arrays.stream(stringArray).forEach(System.out::print);

    }
}
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
  • That's amazing. How the lack of a for loop doesn't break that code I'm not quite sure yet. Also never seen that input.split(" ") thing, and the double colons. – AndoTwo May 09 '15 at 22:44
0

To get the String form of an array, use the Arrays class toString method.

System.out.println(Arrays.toString(stringArray));

Also note the sort method of this class, although in the code's current state each item of your array will be equal to the input line.

copeg
  • 8,290
  • 19
  • 28
  • What is the difference between treating each individual letter in the array as a char and getting the String form? – AndoTwo May 09 '15 at 22:35