This is my first time asking a question on stackoverflow. I've been stumped with this question for the longest time now and I do know what's wrong with my code. Any help/suggestions would be much appreciated. Thank you.
Q1: Write a public static
method named q1 that takes no parameters and has return type boolean
. This method will attempt to open a file named school.txt and returns true
if the file exists and contains the String "test" as a sub-String on any line, and false
if "test" is not found. This method will also return false
if "school.txt" does not exist.
public static boolean q1() {
try{
String fileName = "";
if(fileName == "school.txt");{
for(String line : Files.readAllLines(Paths.get("school.txt"))){
String[] data = line.split(",");{
for(int i = 0; i< data.length; i++) {
if(data[i].contains("test"));{
return true;
}
}
}
}
}
} catch (IOException ex){
ex.printStackTrace();
}
return false;
}