0
import java.util.Comparator;

import components.queue.Queue;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;


public final class QueueSortMain {

public static void main(String[] args) {
        SimpleReader in = new SimpleReader1L();
        SimpleWriter out = new SimpleWriter1L();

        /*
         * Get input file name and open input stream
         */

        out.print("Enter an input file name: ");
        String fileName = in.nextLine();
        SimpleReader file = new SimpleReader1L(fileName);

        /*
         * Get lines from input and output them, unsorted
         */

        Queue<String> q = new Queue1LSort1();
        getLinesFromInput(file, q);
        putLinesToOutput(out, q);

        /*
         * Sort lines into non-decreasing lexicographic order
         */

        Comparator<String> cs = new StringLT();
        q.sort(cs);

        //  Output lines in sorted order

        putLinesToOutput(out, q);

        in.close();
        out.close();

    }
}

I have a file named lines.txt saved under the data folder. I get this error when I try to input lines.txt

Exception in thread "main" java.lang.AssertionError: Violation of: lines.txt exists
    at components.simplereader.SimpleReader1L.<init>(Unknown Source)
    at QueueSortMain.main(QueueSortMain.java:125)  ```

I used the Debugger and the program crashes at

SimpleReader file = new SimpleReader1L(fileName); 
Gdog
  • 1
  • 1
  • Well it looks like `lines.txt` doesn't exist... – Jon Skeet Jul 14 '21 at 20:58
  • Side note for clarification: The error is not "Unknown Source" (from your question's title). It is "AssertionError: Violation of: lines.txt exists" - meaning that an assertion stating that "the file exists" turned out to be false. The "unknown source" message simply means that the source code line number for the line where the error occurred (in the 3rd party library) is not available. Compare that to where your stack trace states "QueueSortMain.java:125" - where you can see the line number for your own code. – andrewJames Jul 14 '21 at 21:37

2 Answers2

0

Following is the code of the constructor you are using for SimpleReader1L (from here). As you can see the error is thrown when the file is not found. This means either that the file does not exist or that you use the wrong path.

Probably, you should look at this answer, where someone explains how you can find out, which path you are using. With it you can simply check the absolute path on your disk for the input file.

/**
 * Constructor for input from given file.
 *
 * @param name
 *            the name of the file or of a URL to input from
 */
public SimpleReader1L(String name) {
    assert name != null : "Violation of: name is not null";
    this.name = name;
    try {
        URL url = new URL(name);
        this.rep =
                new BufferedReader(new InputStreamReader(url.openStream()));
    } catch (MalformedURLException e) {
        // throw new AssertionError("Violation of: " + url + " is valid");
        try {
            this.rep = new BufferedReader(new FileReader(name));
        } catch (FileNotFoundException ex) {
            throw new AssertionError("Violation of: " + name + " exists");
        }
    } catch (FileNotFoundException e) {
        throw new AssertionError("Violation of: " + name + " exists");
    } catch (IOException e) {
        throw new AssertionError("Violation of: " + name + " can be read");
    }
}
JANO
  • 2,995
  • 2
  • 14
  • 29
0

If your file is under data/lines.txt and you pass the incorrect path (lines.txt but the current working directory is not data), then the file is not found. Did you check the path parameter?

taronyu
  • 86
  • 6