在段落数组中查找多组给定单词
Finding multiple set of given words in a paragraph array
我正在字符串数组中搜索单词,如果找到我想要 return 他们的行。
我试图将搜索到的输入划分在一个数组中,然后在段落数组中逐行搜索它。这实际上行不通。
public static void main(String[] args) {
String paragraph[] = new String[10];
String toBeSearched;
String curSearch;
boolean intIndex = false ;
paragraph[0] = "Hello my name is";
paragraph[1] = "Jack the reaper";
paragraph[2] = "what up";
System.out.printf("enter string:");
Scanner myScanner = new Scanner(System.in);
toBeSearched = myScanner.nextLine();
String[] divide = toBeSearched.split(" ");
for(int j = 0 ;j <=10 ; j++) {
curSearch = divide[j];
for (int k = 0; k <=paragraph[j].length() ; k++) {
intIndex = paragraph[j].contains(curSearch);
if(intIndex == true) {
System.out.printf("Found at line %d \n",j );
}
}
}
假设我一次最多可以搜索 10 个词。
假设用户输入:“Hello name the up”
我想要答案:在第 1 行
在第 1 行
在第 2 行
在第 3 行
如果我在该段落的第二个或第三个索引中请求一个词,它不起作用我不明白为什么(没有收到错误消息)
这是一个工作代码,将其与您的代码进行比较并找出问题所在:
public static void main(String[] args)
{
String paragraph[] = {"Hello my name is","Jack the reaper", "what up"};
String toBeSearched;
String curSearch;
boolean intIndex = false ;
System.out.printf("enter string:");
Scanner myScanner = new Scanner(System.in);
toBeSearched = myScanner.nextLine();
String[] divide = toBeSearched.split(" ");
for(int i = 0; i < divide.length ; i ++) {
String word = divide[i];
for(int y = 0; y < paragraph.length ; y++) {
if(paragraph[y].contains(word)) {
System.out.println(word+ "found at line: "+ y);
}
}
}
}
我正在字符串数组中搜索单词,如果找到我想要 return 他们的行。 我试图将搜索到的输入划分在一个数组中,然后在段落数组中逐行搜索它。这实际上行不通。
public static void main(String[] args) {
String paragraph[] = new String[10];
String toBeSearched;
String curSearch;
boolean intIndex = false ;
paragraph[0] = "Hello my name is";
paragraph[1] = "Jack the reaper";
paragraph[2] = "what up";
System.out.printf("enter string:");
Scanner myScanner = new Scanner(System.in);
toBeSearched = myScanner.nextLine();
String[] divide = toBeSearched.split(" ");
for(int j = 0 ;j <=10 ; j++) {
curSearch = divide[j];
for (int k = 0; k <=paragraph[j].length() ; k++) {
intIndex = paragraph[j].contains(curSearch);
if(intIndex == true) {
System.out.printf("Found at line %d \n",j );
}
}
}
假设我一次最多可以搜索 10 个词。
假设用户输入:“Hello name the up” 我想要答案:在第 1 行 在第 1 行 在第 2 行 在第 3 行
如果我在该段落的第二个或第三个索引中请求一个词,它不起作用我不明白为什么(没有收到错误消息)
这是一个工作代码,将其与您的代码进行比较并找出问题所在:
public static void main(String[] args)
{
String paragraph[] = {"Hello my name is","Jack the reaper", "what up"};
String toBeSearched;
String curSearch;
boolean intIndex = false ;
System.out.printf("enter string:");
Scanner myScanner = new Scanner(System.in);
toBeSearched = myScanner.nextLine();
String[] divide = toBeSearched.split(" ");
for(int i = 0; i < divide.length ; i ++) {
String word = divide[i];
for(int y = 0; y < paragraph.length ; y++) {
if(paragraph[y].contains(word)) {
System.out.println(word+ "found at line: "+ y);
}
}
}
}