0

So I need help fixing this code, or more so understanding its faults. The code itself is supposed to read a file and print out the of occurrence of a string of words. However, it seems that it always prints out "Can't find file" even when the .txt file is on my desktop.

import java.util.*;
import java.io.*;

/**
 * Searcher
 */
public class Searcher extends File {

    Scanner scn;

    public Searcher(String filename) {
        super(filename);
    }

    public void search(String input) {

        try {
            scn = new Scanner(this);
            String data = "";

            while (scn.hasNext()) {
                data = scn.nextLine();
            }

            int count = 0, fromIndex = 0;
            while ((fromIndex = data.indexOf(input, fromIndex)) != -1) {
                count++;
                fromIndex++;
            }
            System.out.println("Total occurrences: " + count);

            scn.close();
        } catch (Exception e) {
            System.out.println("Cant find file ");
        }
    }

    public static void main(String[] args) {
        Searcher search = new Searcher("ihaveadream.txt");
        search.search("slavery");
    }
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
Sebastian
  • 15
  • 1
  • 3
  • can you try use full path of "ihaveadream.txt" for constructor parameter? – Mustafa Çil Jul 05 '18 at 12:43
  • 4
    In the catch block use e.printStackTrace(); method to print the root cause of exception which will reveal the actual issue. – Shrinu Namburu Jul 05 '18 at 12:46
  • Related: https://stackoverflow.com/q/1080634 – Ivar Jul 05 '18 at 12:48
  • Possible duplicate of [How to get the Desktop path in java](https://stackoverflow.com/questions/1080634/how-to-get-the-desktop-path-in-java) – SHS Jul 05 '18 at 12:56
  • Where is your application on your file system? You should try something like `Searcher search = new Searcher("C:\User\YourUsername\Desktop\ihaveadream.txt");` if your text file is on the desktop... – deHaar Jul 05 '18 at 13:10

4 Answers4

1

Use the full path for the .txt file or move it into the same folder as the rest of your project. The program won't check the desktop (even if the folder is in the desktop).

Cycl10ps
  • 49
  • 6
0

You can use a more elegant way to read the file using the Stream API:

Files.lines(Paths.get("/home/userName/Desktop/text.txt")) .forEach(System.out::println);

Depending on the operating system, there will be different paths, but there must be an absolute path to the file.

Andrii Torzhkov
  • 271
  • 1
  • 5
  • 15
0

If you do not want to create a file on your OS desktop, you can just create a file on IDE on a package in which the current class is, then indicate the file directory on main method: in my case : src/practice/ihaveadream.txt

public static void main(String[] args) {
        Searcher search = new Searcher("src/practice/ihaveadream.txt");
        search.search("slavery");
    }

I use IntelliJ Idea Community, so you can see how to create a file on Idea: enter image description here

jundev
  • 159
  • 1
  • 1
  • 11
0
use this code 

File file = new File("C:\\Users\\abc\\Desktop\\test.txt");// this is a path of your file

  BufferedReader br = new BufferedReader(new FileReader(file));

  String str;
  while ((str = br.readLine()) != null)
    System.out.println(str);
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15