0

We are suppose to create a method that • The number of lines in the file • The number of words in the file, and • The number of characters in the file excluding blanks. i.e File: xxx.txt has 120 lines, 317 words, 4154 characters

• Search the file line by line for a given string. The output must contain the line number followed by the contents of the line that contains the search argument. For instance given the following as input string: Java, where Java is the search string the output would be something like this:

5: on the island of Java

9: The people of JAVA loves jaVa.

Use the class LineNumberReader for this exercise

I am having a difficult time. I keep getting an I/O error saying stream closed when I am running my code.

This is my code. I have the part of lines, words, characters. But I can't get the second part to work

public String wordCount()
{
    try
    {
        int wordCount = 0,
            numberCount = 0,
            lineCount = 1,
            characterCount = 0;

        String c = " "; 

        FileReader r = new FileReader(f);
        LineNumberReader lnr = new LineNumberReader(r);
        StreamTokenizer t = new StreamTokenizer(r);
        ArrayList <String> results = new ArrayList<String>();


        t.resetSyntax();
        t.whitespaceChars(0, ' ');
        t.wordChars('a','z');
        t.wordChars('A','Z');
        t.wordChars('0','9');

        t.eolIsSignificant(true);

        while(t.nextToken() != StreamTokenizer.TT_EOF)
        {
            switch(t.ttype)
            {
            case StreamTokenizer.TT_NUMBER:
                numberCount++;
                break;
            case StreamTokenizer.TT_WORD:
                characterCount += t.sval.length();
                wordCount++;
                break;
            case StreamTokenizer.TT_EOL:
                lineCount++;
                break;
            case StreamTokenizer.TT_EOF:
                break;
                default:

            }
        }

        r.close();
    BufferedReader bf = new BufferedReader(new FileReader(f));
    BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
    BufferedWriter output = new BufferedWriter(new FileWriter("output.txt"));
    int recCount = 0;
    String record = null;
       while ((record = bf.readLine()) != null) {
          recCount++;
          out.write(recCount + ": " + record);
          out.newLine();
       }
    out.close();


    String ask = "Enter Word";

String find = JOptionPane.showInputDialog(ask);
String word = find;


while(lnr.readLine() != null )
    {
        String line = lnr.readLine();
        if (line.indexOf(word) >= 0)
        {
            results.add(lnr.getLineNumber() + line);
        }
    } 
    lnr.close();


       String str = f.getName() + " has " +  lineCount + " line(s), " +
                 wordCount + " word(s), " +
                 characterCount  + " characters. " + "\n These lines contain"
               + "the word in search of:" ;
       for(int index=0; index<results.size();index++)
       {
           if(results !=null)
           {
               str+= results.get(index) + "\n";
           }
        }

     return str;
    }

I notice it breaks around the this part of code. If I comment the code out it works minus the part I am trying to do.

String ask = "Enter Word";

String find = JOptionPane.showInputDialog(ask);
String word = find;


while(lnr.readLine() != null )
    {
        String line = lnr.readLine();
        if (line.indexOf(word) >= 0)
        {
            results.add(lnr.getLineNumber() + line);
        }
    } 
lnr.close();
Mat
  • 202,337
  • 40
  • 393
  • 406
user1444775
  • 71
  • 1
  • 1
  • 6

2 Answers2

4

In your loop

while(lnr.readLine() != null )

You try to read from lnr, which relies on r, which you closed earlier.

Solution: do not close r before you are done with the file completely. Since you want to open the file again, the easiest way would be to move the above loop right before the r.close() as the loop does not depend on results from out.

Also, when you write

while(lnr.readLine() != null)
{
  String line = lnr.readLine();

You are skipping lines (you read one in the loop condition, which is discarded, then you read the next line into line. You could do this instead:

String line = null;
while((line = lnr.readLine()) != null)
{
  // rest of the loop

this will read the next line, assign it to line and check for null every time the loop is executed

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Thanks. I forgot that I had put the r.close at the top. Now I can't get the second part that I am trying to accomplish to work. Nothing is displaying from the arraylist. – user1444775 Jun 08 '12 at 15:32
0

I think your problem might be that the line

r.close();

is closing your handle on the file. So when you try and read from it later on, you can't see into it anymore. Try deleting that close.

Edit: See Attila's more detailed answer :)

fleeblewidget
  • 63
  • 1
  • 7