简体   繁体   中英

Finding certain words in a paragraph class with each line in an array

I have been trying to make a simple menu where the user can enter a line that they want to add to the paragraph and then search the word(s) that they enter. However, in the case of searching the words (Case 3) if the word that they search is not in the first line it doesn't work (I get no errors) but my code works in a separate file with manual inputs.

Here is my 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;
            }
        }
    }
}

I'm thinking the way that I take the lines in the Case 1 can cause the issue but here is my other file that I tested the Case 3 which doesn't work here

Here is my separate file which I tested Case 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);
            }
        }
        
    }
}

Here when I search for "Hello Jack" it gives me

Hello found at line 0 Jack found at line 1

But in my class above it doesnt work any ideas?

for(int j = 0; j <3 ; j++) {
   paragraph[j] ="Hello my name is" ;
}

This piece of code, changes your paragraph . Therefore, you are searching for "Hello Jack" in

{ "Hello my name is",
  "Hello my name is",
  "Hello my name is" }

Please delete the for loop and try again.

Edit: Your program works fine for the input you gave on my computer, can't reproduce the error. Make sure you compile/run the correct code.

在此处输入图像描述

Edit 2: You have an indexing problem. See the code below;

// 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;

Should be:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM