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

  • Thread starter Thread starter Math Jeans
  • Start date Start date
  • Tags Tags
    File Java
AI Thread Summary
A user encountered issues with a Java program designed to search for a specific word in a file, specifically using "try" as the search term in "Sgrep.java." The main method checks for the correct number of arguments and initializes the Sgrep class. The Sgrep class attempts to read the specified file and search for the word. The user consistently received a "File not found" error despite the code appearing correct.After troubleshooting, the user discovered that the program required the full file path to locate "Sgrep.java," rather than just the file name. This realization highlighted the importance of verifying file paths when encountering errors. The user emphasized the lesson learned about checking basic issues first when debugging code.
Math Jeans
Messages
347
Reaction score
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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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