Solving Java File Not Found Error - "Sgrep.java

  • Java
  • Thread starter Math Jeans
  • Start date
  • Tags
    File Java
In summary, the conversation is about a person having trouble getting a Java program to work. They are trying to create a program that searches for a specific word in a given file, but the program keeps returning "File not found." After some troubleshooting and realizing they needed to specify the full file path, the program finally worked. The person reflects on the lesson they learned from this experience.
  • #1
Math Jeans
349
0
Hello. I am having some trouble getting a java program to work. I am supposed to make a program that takes a word and a file name, and finds all lines in that file that contain that word.

The word that I chose was "try", and the file was "Sgrep.java"

Here is the main method:

Code:
class main {
public static void main(String[] args){
        if(args.length != 2){
            System.out.println("Usage: java Sgrep <string> <filename>");
            return; }
        Sgrep task = new Sgrep(args[0], args[1]);
        System.out.println(task.getFilename());
        System.out.println(task.search());
    }
    }

and here is the class Sgrep:

Code:
import java.io.*;
public class Sgrep {
    String find;
    String file;
    Sgrep(String fin, String fil)
    {
        find=fin;
        file=fil;
    }
    public String getFilename() {
        return file;
    }
    public String search() {
        FileReader read;
        String currentLine;
        String answer = "";
        if(find == null) {return "";}
        try {
            read = new FileReader(file);
        }
        catch (FileNotFoundException e) {return "File not found";}
        LineNumberReader reader = new LineNumberReader(read);
        while(0==0) {
            try {
                currentLine = reader.readLine();
            } catch (IOException ex) {
                return answer;
            }
            if(currentLine.indexOf(find) != -1) {answer = answer + "\n" + 
                    reader.getLineNumber() + ":\t" + currentLine;}            
        }
    }
}

I put a return statement after the FileNotFoundException that prints "File not found", and that is what I have been getting.

Did I do anything wrong that is not allowing the program to find the file? Because it is supposed to work.

I would appreciate any help,
Jeans

This is important, I need this done by this weekend.


EDIT: I got it to work. The assignment sheet had given an example output that showed the file name as Sgrep.java, however, in order to get the program to recognize the file, I had to type in the entire target (C:\Users\User\Sgrep\src\sgrep\Sgrep.java). That is two hours of my life I'll never see again :biggrin:.
 
Last edited:
Technology news on Phys.org
  • #2
Math Jeans said:
EDIT: I got it to work. The assignment sheet had given an example output that showed the file name as Sgrep.java, however, in order to get the program to recognize the file, I had to type in the entire target (C:\Users\User\Sgrep\src\sgrep\Sgrep.java). That is two hours of my life I'll never see again :biggrin:.
I'm sure there is some teachable moment in there. For one, you will never make such a mistake again!

As for others reading this, I think that the lesson is to always check the obvious things first. If you get an error saying "file not found," believe that the program is not finding the file, and try to figure out why.
 

FAQ: Solving Java File Not Found Error - "Sgrep.java

What is the cause of the "File Not Found" error in Java for "Sgrep.java"?

The most common cause of this error is that the file "Sgrep.java" does not exist in the specified location or it has been moved or deleted.

How can I fix the "File Not Found" error for "Sgrep.java"?

To fix this error, you can try the following solutions:
- Make sure that the file "Sgrep.java" exists in the specified location.
- Check if the file has been moved or deleted and restore it to the correct location.
- Check for any spelling mistakes in the file name or file path.
- If you are using an IDE, refresh the project or rebuild it.
- If you are using the command line, navigate to the correct directory before running the program.

Why does the "File Not Found" error for "Sgrep.java" occur even though the file exists?

This error can also occur if the file permissions are not set correctly. Make sure that you have the appropriate read and write permissions for the file.

What should I do if the "File Not Found" error for "Sgrep.java" persists?

If none of the above solutions work, try restarting your computer and then run the program again. If the error still occurs, there may be an issue with your Java installation. Try reinstalling Java and then try running the program again.

How can I prevent the "File Not Found" error for "Sgrep.java" in the future?

To prevent this error from occurring in the future, make sure to double check the file name and location before running the program. Also, avoid moving or deleting the file without updating the file path in your program. Additionally, regularly check for and fix any file permission issues.

Similar threads

Replies
2
Views
1K
Replies
1
Views
1K
Replies
4
Views
3K
Replies
1
Views
1K
Replies
8
Views
2K
Replies
4
Views
3K
Replies
2
Views
1K
Back
Top