从随机用户输入创建字符串数组

时间:2023-01-13 03:39:04

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.

当我打印时,这给了我[Ljava.lang.String; @ 55f96302。

3 个解决方案

#1


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]);   
    }

#2


You are making 2 major mistakes:

你犯了两个重大错误:

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

1)您为每个字符串分配整个输入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().

2)您必须迭代字符串数组的每个元素。在Java8中,可以使用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.

顺便说一句。 String [] stringArray = input.split(“”);会短得多。

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);

    }
}

#3


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

要获取数组的String形式,请使用Arrays类toString方法。

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.

另请注意此类的sort方法,尽管在代码的当前状态中,数组的每个项都将等于输入行。

#1


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]);   
    }

#2


You are making 2 major mistakes:

你犯了两个重大错误:

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

1)您为每个字符串分配整个输入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().

2)您必须迭代字符串数组的每个元素。在Java8中,可以使用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.

顺便说一句。 String [] stringArray = input.split(“”);会短得多。

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);

    }
}

#3


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

要获取数组的String形式,请使用Arrays类toString方法。

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.

另请注意此类的sort方法,尽管在代码的当前状态中,数组的每个项都将等于输入行。