0

I can't figure it out. I'm trying to write out text from my program. It's suppose to a word count program. Show me the number of lines, characters, word count. Then I display the results along with the word the user is searching for and that line.

(i.e. searching java)

line 5: the island of java contains Java

line 9: I love to drink java

It's not displaying text. Its displaying like heiroglyphics.

Line 2: DN{c�<���\$H�Uz�X����h4[����bA.�D��Ja�8^)|��k�ˠ����<Τ���QJ�����P˒��nI"�(��vc�Bi�"&�/�|qI�W6{pa�0��[���[M��;�FU�!}4�x�����{�-��(����V�k@�We֭Tʺ Line 3: �N�U �������Ӣ ͇�?� Line 4: Ӻ鬵�P��D<�}L>��o�V�Ex���Q|�)�'��g�I�B�3b�(�"3�T�7��� �=��s�g�F�;KN���r��_�� ʺ:�� �B�ۢ�s��sP����[6��; �� PK ! ��� N _rels/.rels �(�

public void readFromFile(String filename)
{
    LineNumberReader lineNumberReader = null;
    try {
        lineNumberReader = new LineNumberReader(new FileReader(filename));
        String line = null;
        BufferedWriter output = new BufferedWriter(new FileWriter("output.txt"));
        String ask = "Enter Word";
        String find = JOptionPane.showInputDialog(ask);    
        Scanner scan = new Scanner(new File("test.txt"));
        while ((line = lineNumberReader.readLine()) != null)
        {
          line = scan.nextLine();
          if(line.indexOf(find) >= -1)
            {
             output.write("Line " + lineNumberReader.getLineNumber() +
                    ": " + line);
             output.newLine();
            }
         }// end of while
        output.close();
        } // end of try
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        } 
        catch (IOException ex)
        {
            ex.printStackTrace();
        } 
    finally {    
        try {
            if (lineNumberReader != null) 
                {
                lineNumberReader.close();
                }
            }  // end of try
        catch (IOException ex) 
            {
            ex.printStackTrace();
            }
         }// end of finally
} // end of function
tshepang
  • 12,111
  • 21
  • 91
  • 136
user1444775
  • 71
  • 1
  • 1
  • 6

2 Answers2

1

I don't get why you are doing this :

while ((line = lineNumberReader.readLine()) != null)
        {
          line = scan.nextLine();
          if(line.indexOf(find) >= -1)
            {
             output.write("Line " + lineNumberReader.getLineNumber() +
                    ": " + line);
             output.newLine();
            }
         }// end of while

instead of this :

while ((line = lineNumberReader.readLine()) != null)
        {
          if(line.indexOf(find) >= -1)
            {
             output.write("Line " + lineNumberReader.getLineNumber() +
                    ": " + line);
             output.newLine();
            }
         }// end of while

You don't need 2 readers for this. And I don't understand why one of the reader is reading in a final file and the other one is reading from a file which name is coming from arg

TecHunter
  • 6,091
  • 2
  • 30
  • 47
0

The default OS encoding is used as set in System.getProperty("file.encoding"). You can explicitly pick one.

    final String encoding = "UTF-16LE"; // Or "Cp1252" or "UTF-8"

    lineNumberReader = new LineNumberReader(new InputStreamReader(new FileInputStream(filename), encoding));
    ...
    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.txt"), encoding));
    ...
    Scanner scan = new Scanner(new File("test.txt", encoding));
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • FileReader doesn't take an encoding parameter, you have to use FileInputStream and InputStreamReader – Alex Jun 11 '12 at 13:41
  • @Joop Eggen This does not work as Alex has said. The compiler is saying invalid argument and no consturctor is applicable. – user1444775 Jun 11 '12 at 13:52
  • @Alex thanks; was still wondering what (multi-)byte cpde could deliver the above. Probably the existing file was in UTF16LE. – Joop Eggen Jun 11 '12 at 13:52