1

I'm new to eclipse and Java. I have an H.W assignment and I need to write a class that reads a .txt file.

Before simulating I created a file named "in.txt" in my C directory. In run configuration I wrote "C:\in.txt" as an argument.

I get this error:

java.io.FileNotFoundException: C:\in.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileReader.(Unknown Source)
at Flesch.main(Flesch.java:25)

I have also tried to write "C:\in.txt" and got the same error.

Here is my code:

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Flesch {

    public static void main(String[] args)
    {
        if (args.length != 1)
        {
            System.out.print("error");
            return;
        }       
        BufferedReader mybuffer = null;
        try
        {
            String my_line;
            int words_num=0;
            int senteses=0;
            int verbs=0;
            int word_verbs = 0;
            String word_delimtiters = " ()*&^%$#@!_-=[]{}?;:',.";
            mybuffer = new BufferedReader(new FileReader(args[0]));

            while((my_line = mybuffer.readLine())!= null)//reading the liens
            {
                //counting the number of words
                StringTokenizer token = new StringTokenizer(my_line,word_delimtiters);
                words_num += token.countTokens();
                //counting the verbs
                while (token.hasMoreElements())
                {
                    String word = token.nextToken();
                    word = word.toLowerCase();
                    word_verbs = 0;
                    boolean last_char_is_verb = false;
                    for (int k=0;k < word.length(); k++ )
                    {
                        switch (word.charAt(k)) 
                        {
                            case 'a': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
                            case 'e': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
                            case 'i': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
                            case 'o': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
                            case 'u': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
                            case 'y': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
                            default: last_char_is_verb = false ;
                        }
                    }
                    if (word_verbs == 0)
                        word_verbs = 1;
                    verbs+= word_verbs;
                }

                //counting the number of sentecses
                for(int i=0;i<my_line.length(); i++)
                {
                    if(my_line.charAt(i) == '.' || my_line.charAt(i) == ',' || my_line.charAt(i) == ';' || my_line.charAt(i) == '?' || my_line.charAt(i) == '!')
                    {
                        senteses++;
                    }
                }
            }
            double Flesch = 206.835 - 84.6*(verbs/words_num) -1015*(words_num/senteses);
            System.out.print(Flesch);
            System.in.read();
        }
        catch(IOException e)
        {
            e.printStackTrace();        
        }
        finally
        {
            try
            {
                if (mybuffer != null)
                    mybuffer.close();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();   
            }
        }

    }   


}

I've searched the web for this error and didn't found something helpful. I'm feeling as if I'm missing something basic

VladH
  • 383
  • 4
  • 16
boaz
  • 920
  • 1
  • 13
  • 32
  • I'm sure that the file is not present in your C drive. Check if it was there. – Sri Harsha Chilakapati Nov 06 '13 at 08:28
  • Try putting the txt file directly into your project directory. – Paul Samsotha Nov 06 '13 at 08:30
  • @SriHarshaChilakapati- the file is there, I'm sure – boaz Nov 06 '13 at 08:31
  • If the file is not there. In your code, java.io.FileNotFoundException: will be thrown. Check if the file is there. – Mengjun Nov 06 '13 at 08:31
  • @peeskillet- how do I do that? – boaz Nov 06 '13 at 08:32
  • In the command line, try dragging the file to insert it. Maybe there is a mistake in the filename or it's extension. – Sri Harsha Chilakapati Nov 06 '13 at 08:33
  • @boaz Where are your Eclipse projects saved? Just drag and drop into the file from C:\ to the Eclipse Project folder. When you first open eclipse, it asks for a workspace. Your project folder should be in that workspace folder. Where are you running your program from. The program class file you're running should also be in the project folder in the bin. place your txt in the root directory, just a level above the bin. If that doesn't work, try and put it directly in the bin folder. – Paul Samsotha Nov 06 '13 at 08:35

5 Answers5

2

The backslash in the path may not be a good idea. Try with a "/".

Refer : file path Windows format to java format

Otherwise, the mistake can be about the extension. Refer this : java.io.FileNotFoundException, file not being found

Community
  • 1
  • 1
Julien
  • 2,544
  • 1
  • 20
  • 25
0

Sometimes one slash is not enough so try to provide "c:\ \in.txt" I think it will work fine.

Sheel
  • 847
  • 2
  • 8
  • 20
0

I tried your code and it worked for me using both a forward and backslash. Perhaps it is to do with permissions on that file location. Try moving it to a directory in your user space.

I set the programs argument within eclipse, so perhaps try that if you were previously doing it from the command line. Although from the looks of your error messages, the file name is getting read from the command line OK.

Also, you have a potential bug on the line

double Flesch = 206.835 - 84.6*(verbs/words_num) -1015*(words_num/senteses);

where there can be a divide by zero if words_num or sentenses is zero. You should check for this condition and handle appropriately.

Choc13
  • 796
  • 7
  • 23
0

Try C:\\in.txt. For String literals, \ is escape character. You have to use \\ if you want \ in code.

Barun
  • 1,520
  • 2
  • 12
  • 18
0

Check if you pass the argument correctly.

java Flesch C:\in.txt should work
java Flesch "C:\in.txt" also

Arguments that will cause a FileNotFoundException:
java Flesch " C:\in.txt" (note the whitespace)
java Flesch "C: \in.txt" (note the whitespace)
java Flesch "C:\ in.txt" (note the whitespace)

Peter Walser
  • 15,208
  • 4
  • 51
  • 78