0

I have the files in the src folder. I have another project that is also giving me the same error of not being able to find the file. I have basically been slamming my head against my desk trying to figure out what is wrong.

package IndentChecker;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;

    class BadIndentationException extends RuntimeException {
        BadIndentationException(String error) {

            super(error);
        }
    }

    public class IndentChecker {
        Stack<Integer> indentStack = new Stack<Integer>();

        private int findFirstNonBlank(String line) {
            // return index of first non-blank character
            // return -1 if the line doesn't contain a non-blank character
            for (int i= 0; i < line.length(); i++) {
                if(line.charAt(i) != ' '){
                    return i;
                }
            }
            return -1;
        }

        private void processLine(String line, int lineNumber) {
            int index = findFirstNonBlank(line);
            // Skip blank lines ... i.e. return immediately
            if(index == -1)
                return;
            // If the stack is empty, then push this index onto the stack and return
            if(indentStack.isEmpty())
                indentStack.push(index);
                // If this index > than the top of the stack, then push this index onto the stack and return
            else {
                if(index > indentStack.peek()){
                    indentStack.push(index);
                    return;
                }
                while(indentStack.peek() > index){ // Pop off all Indentation indexes > index
                    indentStack.pop();
                    // At his point the top of the stack should match the current index. If it
                    // doesn't throw a BadIndentationException. In the error message, include the source Line Number
                } if(indentStack.peek() != index)
                    throw new BadIndentationException("error at line: " + lineNumber);
            }
        }

        public void checkIndentation(String fileName) {
            indentStack.clear(); // Clear the stack
            Scanner input = null;
            try {
                input = new Scanner(new File(fileName));
                int lineNumber = 1;
                while (input.hasNextLine()) { // read through the file line by line
                    String line = input.nextLine();
                    System.out.println(lineNumber + ":" + line);
                    processLine(line, lineNumber); // for each line, call processLine to check indentation
                    lineNumber += 1;
                }
                if (indentStack.peek() == 0)
                    System.out.println("***" + fileName + " must be properly indented." + "/n");
            } catch (BadIndentationException error) {
                System.out.println(error);
            } catch (FileNotFoundException e) {
                System.out.println("Can't open file:" + fileName);
            } finally {
                if (input != null)
                    input.close();
            }
        }

        public static void main(String[] args) {
            IndentChecker indentChecker = new IndentChecker();
            for (int i = 0; i < args.length; i++) {
                System.out.println("Processing file: " + args[i]);
                indentChecker.checkIndentation(args[i]);
            }
        }
}

When I run the code I get this message " C:\Users\user.jdks\openjdk-19.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.3\lib\idea_rt.jar=54873:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.3\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 IndentChecker.IndentChecker Error: Could not find or load main class IndentChecker.IndentChecker Caused by: java.lang.ClassNotFoundException: IndentChecker.IndentChecker

Process finished with exit code 1 " tried to replace "dictionary.txt" with absolute and src path. Tried to run File f= new File and system out that, and it still cannot find the file.

  • This is not about your dictionary.txt file, it's about Java not finding class files. How do you try to run your code? If you are on the shell, what command do you run in what folder, and where are the class files relative to that? If you use an IDE, which one, and how do you try to run the code from the IDE? Does your source folder match the package declaration? Seems odd that you have a package named `IndentChecker`. Does your IDE report problems? – Robert Mar 29 '23 at 17:10
  • Apparently this is something to do with IntelliJ. I switched my project over to eclipse and it seems to be working fine. Thank you for your response. – Maryellen Mar 29 '23 at 18:45

0 Answers0