-2

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;
}
yassadi
  • 524
  • 1
  • 9
  • 20

3 Answers3

0

Besides having the semicolon by the end of your if (which means if is not gonna be used), you have used contains(). If your file is just words being separated by colons, you should use data[i].equals("test"). So if your file is like the below file, equals() will do the charm.

hello, world, test, bye,

But if your file is something like this:

hello world, test is in progress, viva la vida, bye,

you need to examine each word separately, so you need to write a method to check each data[i] for the word test.

In any of the cases above, use trim() method so your String will not contain any white spaces so it could be compared with test.

yassadi
  • 524
  • 1
  • 9
  • 20
0

why used ; after if statement it will terminate the condition and when you will set the file name as you initialize with String filename=""; so better to pass the name of file in q1() method comparing the string with == is not recommended use equals method instead

public static boolean q1(String fileName) {


        try{
            //String fileName = "";

            if(fileName.equals("school.txt")){/*no need to put ; otherwise it will terminate here*/
                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;
    }
Roushan
  • 4,074
  • 3
  • 21
  • 38
0
    public static boolean q1()
    {
        try
        {
            String fileName = ""; //you create this, but never set it to anything else, and only use it once, in an operation that does nothing.  might as well not be here

            if (fileName == "school.txt") ; //you have an if statement that will never evaluate to "true" followed by a ; this line essentially does nothing and might as well not be here
            {//unnecessary bracket
                for (String line : Files.readAllLines(Paths.get("school.txt")))// You are reading in all the lines, don't see a logic problem here
                {
                    String[] data = line.split(",");//why are you splitting the lines?  The problem statment says nothing about that.
                    {

                        for (int i = 0; i < data.length; i++)// iterating through each line
                        {
                            if (data[i].contains("test")) ;// another semicolon directly after the if, line might as well not be here
                            {// unnecessary bracket
                                return true;
                            }//closing of unnecessary bracket
                        }
                    }
                }
            }//closing of unnecessary bracket
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return false;
    }

So let's clean this up, remove all the stuff that does nothing, and see if you can find the bug

public static boolean q1()
{
    try
    {
        for (String line : Files.readAllLines(Paths.get("school.txt"))) // You are reading in all the lines, don't see a logic problem here
        {
            String[] data = line.split(","); //why are you splitting the lines?  The problem statment says nothing about that.
            {
                for (int i = 0; i < data.length; i++) // iterating every line
                {
                    return true; //returning true
                }
            }
        }
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
    return false;
}
Kevin
  • 7,162
  • 11
  • 46
  • 70