[英]Java , storing strings into an array
I wish to put the raw questions(rawQuestions) as inputted in this way in command prompt: java Hangman Hello test 123, into the array (questions). 我希望将以这种方式输入的原始问题(rawQuestions)在命令提示符下:java Hangman Hello test 123放入数组(问题)。 I know rawQuestion will not work down in the "store valid questions into an array" part because of
我知道rawQuestion不会在“将有效问题存储到数组中”部分中工作,因为
in the line 在行中
Therefore how should I rewrite it? 因此,我应该如何重写它?
public class Hangman {
//Validation of raw questions
public static boolean isValidQuestion(String rawQuestion){
rawQuestion = rawQuestion.toUpperCase();
boolean vQuestion = true;
for(int i=0; i<rawQuestion.length(); i++){
char c = rawQuestion.charAt(i);
if(!(c>='A' && c<='Z'))
vQuestion = false;
}
return vQuestion;
}
public static void main(String args[]){
boolean vQs[] = new boolean[args.length];
for(int i=0; i<args.length; i++){
String rawQuestion = args[i];
boolean b = isValidQuestion(rawQuestion);
if(b)
System.out.println(rawQuestion + " is a valid question!");
else
System.out.println("Error: " + rawQuestion + ", input must be a word consist of a-z or A-Z");
vQs[i] = b;
}
//count the number of valid questions
int vCount = 0;
for(int i=0; i<vQs.length; i++){
System.out.println(i + " " + vQs[i] );
if(vQs[i])
vCount++;
}
System.out.println("There are " + vCount + " valid questions");
//store valid questions into an array
String questions[] = new String[vCount];
for(vCount=0; vCount<args.length; vCount++){
questions[vCount] = String rawQuestion;
System.out.println( questions[vCount] );
}
}
}
Try this: 尝试这个:
String[] validQuestions = Arrays.stream(args)
.filter(Hangman::isValidQuestion)
.toArray(String[]::new);
int validQuestionCount = validQuestions.length;
But even without Streams, your whole method could be done in one for loop, counting and collecting the valid questions in one go instead of having three separate steps: 但是,即使没有Streams,您也可以在一个for循环中完成整个方法,一次性计算和收集有效问题,而无需执行三个单独的步骤:
List<String> validQuestions = new ArrayList<>();
for (int i = 0; i < args.length; i++)
{
String rawQuestion = args[i];
boolean b = isValidQuestion(rawQuestion);
if (b)
{
validQuestions.add(rawQuestion);
System.out.println(rawQuestion + " is a valid question!");
}
else
{
System.out.println("Error: " + rawQuestion + ", input must be a word consist of a-z or A-Z");
}
}
// count the number of valid questions
System.out.println("There are " + validQuestions.size() + " valid questions");
// store valid questions into an array
String questions[] = validQuestions.toArray(new String[validQuestions.size()]);
This way, you do not have to juggle the index variables yourself, which is hard to understand, especially if somebody else tries to read it. 这样,您不必自己弄乱索引变量,这很难理解,特别是在其他人尝试读取索引变量的情况下。 (Especially the re-use of
vCount
is kinda scary to me) (特别是
vCount
的重复使用对vCount
来说有点吓人)
I fixed something and commented, not sure if it works, i haven't compiled it. 我修复了一些问题并发表了评论,不确定它是否有效,我还没有编译它。
public static void main(String args[]){
boolean vQs[] = new boolean[args.length];
int vCount=0;
for(int i=0; i<args.length; i++){
String rawQuestion = args[i];
if(isValidQuestion(rawQuestion)){
System.out.println(rawQuestion + " is a valid question!");
//Here you have enough data to count the valid questions, three loops are not needed.
++vCount;
vQs[i]=true;
}else{
System.out.println("Error: " + rawQuestion + ", input must be a word consist of a-z or A-Z");
vQs[i]=false;
}
}
System.out.println("There are " + vCount + " valid questions");
//store valid questions into an array
String questions[] = new String[vCount];
int j=0;
for(int i=0; i<args.length; i++){
//You need to iterate for all strings, because vQs[] is long args.length
if(vQs[i]){
//Ok the i-th question was valid, let's move it and print it
questions[j] = args[i];
System.out.println( questions[j] );
++j;
}
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.