使用数组中的每一行查找段落 class 中的某些单词
Finding certain words in a paragraph class with each line in an array
我一直在尝试制作一个简单的菜单,用户可以在其中输入他们想要添加到段落中的一行,然后搜索他们输入的单词。
但是,在搜索单词的情况下(案例 3),如果他们搜索的单词不在第一行,它就不起作用(我没有收到任何错误),但我的代码在一个单独的文件中使用手动输入。
这是我的 class
public class Paragraph {
String[] lines;
int lineCount;
public Paragraph(String[]lines,int lineCount) {
this.lines = lines;
this.lineCount = lineCount;
}
public static void main(String[] args) {
int lineCount=0 , i = 0;
String[] lines = new String[10];
String curline ,search;
boolean loop= true;
Paragraph parag = new Paragraph(lines, lineCount);
while(loop) {
Scanner myScanner = new Scanner(System.in);
System.out.println();
System.out.printf("%s\n","1) Enter a new line");
System.out.printf("%s\n","2) Display the paragraph");
System.out.printf("%s\n","3) Search for word");
System.out.printf("%s\n","4) Exit");
int choice= myScanner.nextInt();
myScanner.nextLine();
switch(choice) {
case 1:
System.out.println("Enter your line:");
curline = myScanner.nextLine();
lineCount = i;
System.out.println("Done!!");
parag.lines[i] = curline + " ";
parag.lineCount = i;
i++;
break;
case 2:
int index;
for(index=0; index<i; index++) {
System.out.printf("(%d) %s\n",index+1, lines[index]);
}
break;
case 3:
System.out.println("Enter pharese to be searched:");
search = myScanner.nextLine();
String toBeSearched = search;
String[] divide = toBeSearched.split(" ");
for(int i1 = 0; i1 < divide.length ; i1 ++) {
String word = divide[i1];
for(int y = 0; y < lineCount; y++) {
if(lines[y].contains(word)) {
System.out.println(word + " found at line: "+ y);
}
}
}
break;
case 4:
loop = false;
}
}
}
}
我在想我在案例 1 中采用的方式可能会导致问题,但这是我测试案例 3 的另一个文件,它在这里不起作用
这是我测试案例 3 的单独文件
public static void main(String[] args) {
String toBeSearched;
boolean intIndex = false ;
String paragraph[] = {"Hello my name is","Jack the reaper", "what up"};
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);
}
}
}
}
在这里,当我搜索“Hello Jack”时,它给了我
在第 0 行找到你好
在第 1 行找到 Jack
但是在我上面的 class 它没有任何想法吗?
for(int j = 0; j <3 ; j++) {
paragraph[j] ="Hello my name is" ;
}
这段代码,改变了你的paragraph
。因此,您在
中搜索 "Hello Jack"
{ "Hello my name is",
"Hello my name is",
"Hello my name is" }
请删除for循环并重试。
编辑:对于您在我的计算机上提供的输入,您的程序运行良好,无法重现错误。请确保您 compile/run 密码正确。
编辑 2:您有索引问题。看下面的代码;
// When you enter the first line...
case 1:
System.out.println("Enter your line:");
curline = myScanner.nextLine();
lineCount = i; // i is 0, but we have 1 line
System.out.println("Done!!");
parag.lines[i] = curline + " "; // parag[0] is set, correct
parag.lineCount = i; // i is 0, but we have 1 line
i++;
break;
应该是:
case 1:
System.out.println("Enter your line:");
curline = myScanner.nextLine();
System.out.println("Done!!");
parag.lines[i] = curline + " "; // parag[0] is set, correct
i++;
parag.lineCount = i; // i is 1, correct
lineCount = i; // i is 1, correct
break;
我一直在尝试制作一个简单的菜单,用户可以在其中输入他们想要添加到段落中的一行,然后搜索他们输入的单词。 但是,在搜索单词的情况下(案例 3),如果他们搜索的单词不在第一行,它就不起作用(我没有收到任何错误),但我的代码在一个单独的文件中使用手动输入。
这是我的 class
public class Paragraph {
String[] lines;
int lineCount;
public Paragraph(String[]lines,int lineCount) {
this.lines = lines;
this.lineCount = lineCount;
}
public static void main(String[] args) {
int lineCount=0 , i = 0;
String[] lines = new String[10];
String curline ,search;
boolean loop= true;
Paragraph parag = new Paragraph(lines, lineCount);
while(loop) {
Scanner myScanner = new Scanner(System.in);
System.out.println();
System.out.printf("%s\n","1) Enter a new line");
System.out.printf("%s\n","2) Display the paragraph");
System.out.printf("%s\n","3) Search for word");
System.out.printf("%s\n","4) Exit");
int choice= myScanner.nextInt();
myScanner.nextLine();
switch(choice) {
case 1:
System.out.println("Enter your line:");
curline = myScanner.nextLine();
lineCount = i;
System.out.println("Done!!");
parag.lines[i] = curline + " ";
parag.lineCount = i;
i++;
break;
case 2:
int index;
for(index=0; index<i; index++) {
System.out.printf("(%d) %s\n",index+1, lines[index]);
}
break;
case 3:
System.out.println("Enter pharese to be searched:");
search = myScanner.nextLine();
String toBeSearched = search;
String[] divide = toBeSearched.split(" ");
for(int i1 = 0; i1 < divide.length ; i1 ++) {
String word = divide[i1];
for(int y = 0; y < lineCount; y++) {
if(lines[y].contains(word)) {
System.out.println(word + " found at line: "+ y);
}
}
}
break;
case 4:
loop = false;
}
}
}
}
我在想我在案例 1 中采用的方式可能会导致问题,但这是我测试案例 3 的另一个文件,它在这里不起作用
这是我测试案例 3 的单独文件
public static void main(String[] args) {
String toBeSearched;
boolean intIndex = false ;
String paragraph[] = {"Hello my name is","Jack the reaper", "what up"};
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);
}
}
}
}
在这里,当我搜索“Hello Jack”时,它给了我
在第 0 行找到你好 在第 1 行找到 Jack
但是在我上面的 class 它没有任何想法吗?
for(int j = 0; j <3 ; j++) {
paragraph[j] ="Hello my name is" ;
}
这段代码,改变了你的paragraph
。因此,您在
"Hello Jack"
{ "Hello my name is",
"Hello my name is",
"Hello my name is" }
请删除for循环并重试。
编辑:对于您在我的计算机上提供的输入,您的程序运行良好,无法重现错误。请确保您 compile/run 密码正确。
编辑 2:您有索引问题。看下面的代码;
// When you enter the first line...
case 1:
System.out.println("Enter your line:");
curline = myScanner.nextLine();
lineCount = i; // i is 0, but we have 1 line
System.out.println("Done!!");
parag.lines[i] = curline + " "; // parag[0] is set, correct
parag.lineCount = i; // i is 0, but we have 1 line
i++;
break;
应该是:
case 1:
System.out.println("Enter your line:");
curline = myScanner.nextLine();
System.out.println("Done!!");
parag.lines[i] = curline + " "; // parag[0] is set, correct
i++;
parag.lineCount = i; // i is 1, correct
lineCount = i; // i is 1, correct
break;