Just started to learn Java. Help me to understand. Created a primitive code that takes a string from the user, puts it into an array and divides it into words. How now any word from this array to check whether it is in a specific text file? Enough simple design, just to understand the principle. If the word was found "Hooray, found," if not, "The word was not found." Thank.

import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean(name="user") @SessionScoped public class UserTextBean implements Serializable { public String txt; public String txtend; public String getTxtend() { txtend = txt.substring(0, 30); String[] output = txtend.split(" "); return output[1]; } public void setTxtend(String txtend) { this.txtend = txtend; } public String getTxt() { return txt; } public void setTxt(String txt){ this.txt = txt; } } 
  • Open the file, load its contents into String , then use String.substring(...) . - post_zeew

2 answers 2

 public class someClass{ public static final String content; String words[] = SomeClass2.getWords(); //Какой-то класс со статическим методом выдающим массив слов из введенного пользователем предложения public someClass(){ try{ content = new String(Files.readAllBytes(Paths.get("url"))); }catch(IOException e){} } public static void main(String args[]){ boolean isExists = false; isExists = content.insexOf(words[индекс слова]); if(isExists) System.out.println("Найдено совпадение"); } } 

    Perhaps this will do:

     try { String text = ""; FileReader fileReader = new FileReader("D:/Data/prototype.txt"); //Открываем поток для вычисления условия BufferedReader counter = new BufferedReader(fileReader); //Открываем поток для чтения файла BufferedReader br = new BufferedReader(fileReader); while (counter.readLine() != null) { text += br.readLine();//считываем текст } if (text.contains("TestData")) { JOptionPane.showMessageDialog(null, "Ура, нашёл!"); } else { JOptionPane.showMessageDialog(null, "Слово не найдено."); } } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); }