help in completing a method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • glitchy
    New Member
    • May 2009
    • 4

    help in completing a method

    Hey all, need help in a question

    Complete the method below that is passed two strings, a name of a file and a target line. This method returns true if the file contains at least one line that matches the target line, and false otherwise. Note your method must include code to properly do the steps needed for file input.

    public boolean containsLine (String filename, String target) {

    any ideas?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by glitchy
    any ideas?
    Yes: read the very first article in this forum group; it contains a link that allows you to download the entire API documentation. Download it and start reading about BufferedReaders , FileInputStream s and the String class. They have all the methods you need.

    kind regards,

    Jos

    Comment

    • glitchy
      New Member
      • May 2009
      • 4

      #3
      something like this?

      Code:
      public boolean containsLine (String filename, String target) {
      try {
      BufferedReader in = new BufferedReader(new FileReader(filename));
      String line = null;
      while((line = in.readLine()) != null) {
      if(target.equals(line))return true;
      }
      }catch(IOException e){}
      return false;
      }
      Last edited by JosAH; May 10 '09, 03:38 PM. Reason: fixed the [code] ... [/code] tags

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Yep, sort of; you only forgot to close your file when you're done with it; nothing a small finally { ... } block can't cure.

        kind regards,

        Jos

        Comment

        Working...