如何在 Java 中创建、填充和打印字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19456509/
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
How to create, populate, and print a string array in Java
提问by censortrip
I'm having trouble populating a String
array from user input data. Here is a simple example of my issue:
我在String
从用户输入数据填充数组时遇到问题。这是我的问题的一个简单示例:
import java.util.Scanner;
public class TestArray {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1 and 5");
int number = input.nextInt();
System.out.println("Now enter " + number + " names");
String names = input.next();
String[] nameList = new String[number];
for(int i = 0; i < number; i++){
nameList[i] = input.next();
System.out.println(nameList[i]);
}
}
}
I do not know why this is only printing out the last name entered. What I ultimately am looking for is to print out every name. I have also tried using input.nextLine()
which did not work.
我不知道为什么这只是打印出输入的姓氏。我最终要寻找的是打印出每个名字。我也试过使用input.nextLine()
which 没有用。
Any tips or pointers would be greatly appreciated.
任何提示或指示将不胜感激。
采纳答案by But I'm Not A Wrapper Class
Just have a seperate loop after entering names to print them all out:
输入名称后只需有一个单独的循环即可将它们全部打印出来:
import java.util.Scanner;
public class TestArray {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1 and 5");
int number = input.nextInt();
System.out.println("Now enter " + number + " names");
//NOTE: You may want to remove this line since it will mess up your data.
//String names = input.next();
String[] nameList = new String[number];
for(int i = 0; i < number; i++){
nameList[i] = input.next();
}
//print separate here.
System.out.println("Here are the names you entered: ");
for(int i = 0; i<number; i++){
System.out.println(nameList[i]);
}
}
}
回答by Embattled Swag
What's the purpose of
目的是什么
String names = input.next();
?
?
You don't use names
at all and are catching the next input after the number because of it.
您根本不使用names
,因此正在捕获数字之后的下一个输入。
It should work if you remove it...
如果你删除它应该可以工作......
回答by Bucket
Your first input.next()
call (after the prompt, "Now enter X names") is skewing your data. Just remove it.
您的第一次input.next()
调用(在提示“现在输入 X 名”之后)正在扭曲您的数据。只需将其删除。
public class TestArray {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1 and 5");
int number = input.nextInt();
System.out.println("Now enter " + number + " names");
String[] nameList = new String[number];
for(int i = 0; i < number; i++){
nameList[i] = input.next();
System.out.println(nameList[i]);
}
}
}
回答by Tom Swifty
The line
线
String names = input.next();
Is causing your problem. If you take that out, the program performs as you describe it should. Alternatively you could populate your array in one loop, and then loop through the list afterward printing them. This depends on what exactly you're looking to do.
导致你的问题。如果你把它去掉,程序就会按照你描述的那样执行。或者,您可以在一个循环中填充数组,然后在打印它们之后循环遍历列表。这取决于您究竟要做什么。
import java.util.Scanner;
public class test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1 and 5");
int number = input.nextInt();
System.out.println("Now enter " + number + " names");
//String names = input.next();
String[] nameList = new String[number];
for(int i = 0; i < number; i++){
nameList[i] = input.next();
System.out.println(nameList[i]);
}
}
}