title: Display searched text with line numbers in a text file
body: <p>So I've been working on a program that will display the line number and the line itself of the searched text string. If I search dog, and I have lines in my text file that contain the word dog, those lines and line numbers should be shown. I also have created a method that counts the characters, words, and lines of a text file. However, the problem I am having is that whenever I run my program I don't get the line numbers with the lines of the searched text. I successfully get the text from the text file in the console and I successfully get the number of lines, words, etc.</p>
  
  <p>Here's my written code, I am guessing it has to do something with the fact that I don't have a "return results;" statement, but I am not sure where to put it, and if I add it to the end of "+ characters + " characters. "" line by doing "+ results", it just gives me empty brackets.</p>
  
  <p>Maybe I am doing something wrong? Perhaps something to do with closing the file and stream, not sure. Please help, I've tried moving stuff around but no luck.</p>
  
  <pre><code>public String words() {
      try {
          int words = 0;
          int numbers = 0;
          int lines = 1;
          int characters = 0;
          int total = 0;
  
          String c = " ";
  
          FileReader r = new FileReader(file1);
          LineNumberReader lnr = new LineNumberReader(r);
          StreamTokenizer t = new StreamTokenizer(r);
          ArrayList&lt;String&gt; results = new ArrayList&lt;String&gt;();
  
          t.resetSyntax();
          t.wordChars('0', '9');
          t.wordChars('A', 'Z');
          t.wordChars('a', 'z');
          t.whitespaceChars(0, ' ');
  
          t.eolIsSignificant(true);
  
          while (t.nextToken() != StreamTokenizer.TT_EOF) {
              switch (t.ttype) {
                  case StreamTokenizer.TT_NUMBER:
                      numbers++;
                      break;
                  case StreamTokenizer.TT_WORD:
                      characters += t.sval.length();
                      words++;
                      break;
                  case StreamTokenizer.TT_EOL:
                      lines++;
                      break;
                  case StreamTokenizer.TT_EOF:
                      break;
                  default:
  
              }
          }
  
          FileInputStream fstream = new FileInputStream(file1);
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  
          String strLine;
  
          while ((strLine = br.readLine()) != null) {
              System.out.println(strLine);
          }
  
          br.close();
  
          String ask = "Enter Word";
  
          String find = JOptionPane.showInputDialog(ask);
          String word = find;
  
          String line = null;
          while ((line = lnr.readLine()) != null) {
              if (line.indexOf(word) &gt;= 0) {
                  results.add(lnr.getLineNumber() + line);
              }
          }
  
          r.close();
  
          total = numbers + words;
  
          lnr.close();
  
          return file1.getName() + " has " + lines + " lines, "
                  + total + " words, "
                  + characters + " characters. ";
      } catch (IOException e) {
          display(e.toString(), "Error");
      }
  
      return " ";
  
  }
  </code></pre>
  
  <p>Here's the main class if needed:</p>
  
  <pre><code>import java.io.*;
  import java.util.ArrayList;
  import javax.swing.*;
  
  public class BasicFile {
  
  File file1;
  JFileChooser selection;
  File file2 = new File(".", "Backup File");
  
  public BasicFile() {
      selection = new JFileChooser(".");
  }
  
  public void selectFile() {
      int status = selection.showOpenDialog(null);
  
      try {
          if (status != JFileChooser.APPROVE_OPTION) {
              throw new IOException();
          }
          file1 = selection.getSelectedFile();
  
          if (!file1.exists()) {
              throw new FileNotFoundException();
          }
      } catch (FileNotFoundException e) {
          JOptionPane.showMessageDialog(null, "File Not Found ", "Error", JOptionPane.INFORMATION_MESSAGE);
      } catch (IOException e) {
          System.exit(0);
      }
  }
  
  public void backupFile() throws FileNotFoundException {
      DataInputStream in = null;
      DataOutputStream out = null;
      try {
          in = new DataInputStream(new FileInputStream(file1));
          out = new DataOutputStream(new FileOutputStream(file2));
  
          try {
              while (true) {
                  byte data = in.readByte();
                  out.writeByte(data);
              }
          } catch (EOFException e) {
              JOptionPane.showMessageDialog(null, "File has been backed up!",
                      "Backup Complete!", JOptionPane.INFORMATION_MESSAGE);
          } catch (IOException e) {
              JOptionPane.showMessageDialog(null, "File Not Found ",
                      "Error", JOptionPane.INFORMATION_MESSAGE);
          }
      } finally {
          try {
              in.close();
              out.close();
          } catch (Exception e) {
              display(e.toString(), "Error");
          }
      }
  
  }
  
  boolean exists() {
      return file1.exists();
  }
  
  public String toString() {
      return file1.getName() + "\n" + file1.getAbsolutePath() + "\n" + file1.length() + " bytes";
  }
  
  public String words() {
      try {
          int words = 0;
          int numbers = 0;
          int lines = 1;
          int characters = 0;
          int total = 0;
  
          String c = " ";
  
          FileReader r = new FileReader(file1);
          LineNumberReader lnr = new LineNumberReader(r);
          StreamTokenizer t = new StreamTokenizer(r);
          ArrayList&lt;String&gt; results = new ArrayList&lt;String&gt;();
  
          t.resetSyntax();
          t.wordChars('0', '9');
          t.wordChars('A', 'Z');
          t.wordChars('a', 'z');
          t.whitespaceChars(0, ' ');
  
          t.eolIsSignificant(true);
  
          while (t.nextToken() != StreamTokenizer.TT_EOF) {
              switch (t.ttype) {
                  case StreamTokenizer.TT_NUMBER:
                      numbers++;
                      break;
                  case StreamTokenizer.TT_WORD:
                      characters += t.sval.length();
                      words++;
                      break;
                  case StreamTokenizer.TT_EOL:
                      lines++;
                      break;
                  case StreamTokenizer.TT_EOF:
                      break;
                  default:
  
              }
          }
  
          FileInputStream fstream = new FileInputStream(file1);
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
  
          String strLine;
  
          while ((strLine = br.readLine()) != null) {
              System.out.println(strLine);
          }
  
          br.close();
  
          String ask = "Enter Word";
  
          String find = JOptionPane.showInputDialog(ask);
          String word = find;
  
          String line = null;
          while ((line = lnr.readLine()) != null) {
              if (line.indexOf(word) &gt;= 0) {
                  results.add(lnr.getLineNumber() + line);
              }
          }
  
          r.close();
  
          total = numbers + words;
  
          lnr.close();
  
          return file1.getName() + " has " + lines + " lines, "
                  + total + " words, "
                  + characters + " characters. ";
      } catch (IOException e) {
          display(e.toString(), "Error");
      }
  
      return " ";
  
  }
  
  void display(String msg, String s) {
      JOptionPane.showMessageDialog(null, msg, s, JOptionPane.ERROR_MESSAGE);
  }
  
  }
  </code></pre>