我是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);
}
}
}
}
答案 0 :(得分:3)
解决方案中不需要字符串数组。
尝试一下:-
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");
}
答案 1 :(得分:1)
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);
}
}
答案 2 :(得分:1)
String [] sentence
。在第一个for块之外不可见。在for循环的迭代过程中一遍又一遍地创建句子数组。从命令行获取String不需要循环。
编辑了我的答案,并删除了任何for循环,Arrays.asList()
将使用words
数组,并从结果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));
}
}
答案 3 :(得分:1)
您只需要在for循环之外声明句子数组,就目前而言,问题是范围。For more on the scope of a variable in java 。另外,这不是您打算要做的,您打算将输入作为命令行。
因此,您将获得的输入将以String []参数形式出现。有关命令行参数check here的更多信息。
答案 4 :(得分:1)
我认为您遇到范围问题。在您的第一个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++;
}
}
乌龟
答案 5 :(得分:1)
为了使您的代码正常工作,我进行了以下更改。请注意您输入的字符串存储不正确。在您的代码中,整个代码被存储为数组中的第一个索引。您不需要第一个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);
}
}
}
}