[英]Storing user input in a string array
我是Java的初学者。 我尝试编写一个程序以从命令行参数中读取一系列单词,并找到给定单词的第一个匹配项的索引。 像用户可以输入“我爱苹果”,给定的单词是“苹果”。 程序将显示“'apple'的第一个匹配项的索引为2”。
到目前为止,我所做的没有用。 我将输入存储到字符串数组的方式不正确吗?
import java.util.Scanner;
public class test {
public static void main(String [] args) {
System.out.println("Enter sentence: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
int num=1;
String sentence[]=new String[num];
for(int i=0; i< num; i++) {
sentence[i] = input; // store the user input into the array.
num = num+1;
}
System.out.println("Enter the given words to find the index of its first match: ");
Scanner sc2 = new Scanner(System.in);
String key = sc2.next();
for(int j=0; j<num; j++) {
while (sentence[j].equals(key)) {
System.out.println("The index of the first match of "+key+" is "+j);
}
}
}
}
解决方案中不需要字符串数组。
尝试这个 :-
System.out.println("enter sentence ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println("enter the given word to fin the index ");
sc = new Scanner(System.in);
String toBeMatched = sc.nextLine();
if (input.contains(toBeMatched)) {
System.out.println("index is " + input.indexOf(toBeMatched));
} else {
System.out.println("doesn't contain string");
}
String[] a = sc. nextLine(). split(" ");
这将扫描新行的输入,并将每个由空格分隔的字符串分隔到每个数组中。
System.out.println("Enter sentence: ");
Scanner sc = new Scanner(System.in);
String[] sentence = sc. nextLine(). split(" ");
System.out.println("Enter the given words to find the index of its first match: ");
String key = sc.nextLine();
for(int j=0; j<num; j++) {
if (sentence[j].matches(key)) {
System.out.println("The index of the first match of "+ key +" is "+ j);
}
}
String [] sentence
。 在第一个for块之外不可见。 在for循环的迭代过程中,一遍又一遍地创建句子数组。 从命令行获取String不需要循环。
编辑了我的答案,并删除了任何for循环, Arrays.asList()
将使用words
array并从结果List
获取单词的索引。
public static void main(String[] args) throws IOException { System.out.println("Enter sentence: "); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); System.out.println("Enter the given word to find the index of its first match: "); Scanner wordInput = new Scanner(System.in); String key = wordInput.next(); String[] words = input.split(" "); int occurence = Arrays.asList(words).indexOf(key); if(occurence != -1){ System.out.println(String.format("Index of first occurence of the word is %d", occurence)); } }
您只需要在for循环外声明句子数组,就目前而言,这个问题是有问题的。 有关java中变量范围的更多信息 。 另外,这不是您打算要做的,您打算将输入作为命令行。
我认为您有范围问题。 在您的第一个forloop中声明并实例化了句子[]。 尝试将声明移出循环,您应该消除该错误。
我还注意到了一些错误。 你可以试试这个
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Enter Sentence");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String sentence[] = input.split("\\s");
System.out.println("Enter Word");
Scanner sc2 = new Scanner(System.in);
String key = sc2.next();
int index = 0;
for(String word : sentence)
{
if(word.equals(key))
{
System.out.println("The index of the first match of " + key + " is " + index);
break;
}
index++;
}
}
龟
我进行了以下更改以使您的代码正常工作。 请注意您输入的字符串存储不正确。 在您的代码中,整个代码被存储为数组中的第一个索引。 您不需要第一个for-loop
因为我们可以使用函数.split()
将每个单词区分为数组中的不同索引。 其余代码保持原样。
public class ConfigTest {
public static void main(String[] args) {
System.out.println("Enter sentence: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
// Use this to split the input into different indexes of the array
String[] sentence = input.split(" ");
System.out.println("Enter the given words to find the index of its first match: ");
Scanner sc2 = new Scanner(System.in);
String key = sc2.next();
for (int i = 0; i < sentence.length; i++) {
if (sentence[i].equals(key)) {
System.out.println("The index of the first match of " + key + " is " + i);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.