在 Java 中获取字符串数组的用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29360534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting user input for string array in Java
提问by IRGeekSauce
I have several methods I will be using in this program. I'm just wanting a little help with one part. I have to make a program that prompts a user to enter the number of elements to be sorted. Once that number is entered, it will prompt the user again to enter the elements themselves. They must be strings. I wrote some quick code to see how this is going to work, but I used ints instead of strings. I can't figure out how to get the program to take a string rather than an int. It would look like this on the console: Enter number of elements: 4
我将在这个程序中使用几种方法。我只是想对某一部分提供一点帮助。我必须制作一个程序,提示用户输入要排序的元素数量。输入该数字后,它会再次提示用户自己输入元素。它们必须是字符串。我写了一些快速代码来看看这是如何工作的,但我使用了整数而不是字符串。我不知道如何让程序接受一个字符串而不是一个整数。它在控制台上看起来像这样:输入元素数:4
Element 1: Apple
Element 2: Banana
Element 3: Orange
Element 4: Kiwi
Then it would sort them in ascending order. I haven't reached the sorting part yet, so it's irrelevant right now. I'm just wanting to know how in the world I can loop my Element 1:, Element 2:, etc with a String as input.
然后它会按升序对它们进行排序。我还没有到达排序部分,所以现在无关紧要。我只是想知道我到底如何使用字符串作为输入来循环我的 Element 1:、Element 2: 等。
Note: I cannot use the Arrays class, which makes this more difficult for me.
注意:我不能使用 Arrays 类,这对我来说更加困难。
Here's what I have using ints:
这是我使用整数的内容:
import java.util.*;
public class Sort_as_Inserted {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements to be sorted: ");
int num_of_elements = input.nextInt();
int[] num = new int[num_of_elements];
for (int i = 0; i < num_of_elements; i++)
{
System.out.print("Element" + (i + 1) + ": ");
num[i] = input.nextInt();
}
System.out.println();
}//end Main
}//end Class
采纳答案by Moustache_Me_A_Question
Instead of input.nextInt() you can just put input.next() and that will get the next immediate string value entered. You can also do input.nextLine() if I remember right and that will give you an entire line as a string value which can be useful for when reading in a large text file since you will be able to tackle it line by line while reading it in.
您可以只输入 input.next() 而不是 input.nextInt() ,这将获得输入的下一个立即字符串值。如果我没记错的话,您也可以执行 input.nextLine() ,这将为您提供整行作为字符串值,这在读取大型文本文件时非常有用,因为您可以在阅读时逐行处理它它在。
EDIT:
编辑:
This is a little tricky without using an array. You can put them into a large string value. Appending a String Builder object would be better since it is mutable but that is probably not in your current scope.
如果不使用数组,这有点棘手。您可以将它们放入一个大字符串值中。附加 String Builder 对象会更好,因为它是可变的,但这可能不在您当前的范围内。
String elements ="";
for(int i = 0; i <= num_of_elements; i++){
System.out.println("Please enter an element");
elements += input.next() + " ";
}
回答by Gatsby Great
You can try this example to see the outcome. Puting the below code in a loop can solve the problem. Within your loop you can add each input to a Collection. (You do not need to have Scanner in = new Scanner(System.in)
in the loop)
你可以试试这个例子看看结果。将以下代码放入循环中可以解决问题。在循环中,您可以将每个输入添加到集合中。(你不需要Scanner in = new Scanner(System.in)
在循环中)
Scanner in = new Scanner(System.in);
System.out.println("Enter element: ");
el = in.nextLine(); // reads string
To sort you can look into HashSet and TreeSetfrom the documentation
要排序,您可以从文档中查看 HashSet 和TreeSet
EDIT: Maybe something like this would do.
编辑:也许这样的事情会做。
TreeSet<String> ts = new TreeSet<String>();
Scanner in = new Scanner(System.in);
String element = “”;
for(int i = 0; i < 4; i++){
element =in.nextLine();
ts.add(element);
}
回答by BlackHatSamurai
To get the input as a String
you need to use:
要获取输入,String
您需要使用:
String num_of_elements = input.nextLine();
Once you have that, you can convert it to an int
by using:
有了它后,您可以int
使用以下方法将其转换为 an :
Integer.parseInt(num_of_elements);
Your code might look something like this:
您的代码可能如下所示:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements to be sorted: ");
String num_of_elements = input.nextLine();
String[] num = new String[Integer.parseInt(num_of_elements)];
for (int i = 0; i < Integer.parseInt(num_of_elements); i++)
{
System.out.print("Input Element" + (i + 1) + ": ");
num[i] = input.nextLine();
}
int index = 0;
for(String element : num){
System.out.println("Element at index " + index + " = " + element);
}
input.close();
}//end Main
回答by Saurabh Jhunjhunwala
As per my understanding, you are trying to read a number of string input values, but do not want to use an Array.
please find the below program, where I am using a list interface and any of its implementation classes (could be arraylist, linkedlist etc.)
this program will store the input values and then collections class will sort it for you. One more important thing, you should always close the scanner.
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements to be sorted: ");
int num_of_elements = input.nextInt();
List elementList = new LinkedList();
for (int i = 0; i < num_of_elements; i++) {
System.out.print("Element" + (i + 1) + ": ");
elementList.add(input.next());
}
input.close();
System.out.println(elementList);
Collections.sort(elementList);
System.out.println(elementList);
}
}