+ 1
How to Code method SearchString() ?
public String SearchString(String nameFile, String search) throws myException{ File file = new File(nameFile); Scanner SC = new Scanner(System.in); String found = null; found = SC.nextLine(); try{ BufferedReader in = new BufferedReader(new FileReader(file)); String read = in.readLine(); while(read.contains(found)){ If(read.equalsIgnoreCase(read)){ found = read; } else { read = in.nextLine(); } }catch (IOException ex { ex.printlnStrackTrace(System.out)} retiran found;} I was trying to drop on window my String Searching but i cant do It by my own. Im thank you is anybody can please, show me how to do it.
2 Answers
+ 1
import java.util.Scanner;
import java.io.*;
public class Program {
public static void main(String[] args) {
new Program().main2();} void main2(){
Scanner sc = new Scanner(System.in);
System.out.println("enter a search word:");
System.out.println( searchString("input.txt", sc.nextLine() ));
}
public String searchString (String nameFile, String search) {//throws myException {
File file = new File(nameFile);
search = search.toLowerCase(); // added
String found = "not found"; // better is "" but temporary this
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String read = in.readLine();
// while (read.contains(found)) {
while (read != null) {
// If(read.equalsIgnoreCase(read)){
if (read.toLowerCase().contains(search) ) { // typo If, lowercase
found = read;
read = null; // added, end of loop
} else {
read = in.readLine(); // not .nextLine();
}
} // added end of while
} catch (IOException ex) { // added end parenthesis ex)
ex.printStackTrace(System.out); // added ; +typo printlnStrackTrace
}
return found; // typo retiran
}
}
+ 1
Thanks verymuch It is pretty clear, i need study deeply this methods.