0

I have the following config file that I am trying to use to set global variables in my program

OUTPUT_FILE_PATH = filename2.txt
MAX_CRAWL_DEPTH = 2
NUMBER OF CRAWLERS = 10
SEED_URL = https://www.lehigh.edu/home
DOMAIN = lehigh.edu
DELAY = 10

To parse this text file I am using this

String line = "";
            BufferedReader buffRead = new BufferedReader(new FileReader((CONFIG_FILE_PATH)));
            Scanner charlie = new Scanner(line);

            String varName;
            while ((line = buffRead.readLine()) != null){
                System.out.println(line);
                charlie.useDelimiter(" = ");
                varName = charlie.next();
                System.out.println(varName);



                if (varName == "OUTPUT_FILE_PATH")
                    OUTPUT_FILE_PATH = charlie.next();
                else if (varName == "MAX_CRAWL_DEPTH")
                    MAX_CRAWL_DEPTH = charlie.nextInt();
                else if (varName == "NUMBER_OF_CRAWLERS")
                    NUMBER_OF_CRAWLERS = charlie.nextInt();
                else if (varName == "SEED_URL")
                    SEED_URL = charlie.next();
                else if (varName == "DOMAIN")
                    DOMAIN = charlie.next();
                else if (varName == "DELAY")
                    delay = charlie.nextInt();
            }

I get this output when running the code

OUTPUT_FILE_PATH = filename2.txt
Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1478)
        at Globals.setGlobals(Globals.java:44)

Line 44 is where I set varName = charlie.next(); Any reason why this would be wrong? Any other tips on how to go about parsing this file? I feel like my if statements is not the best way to do it.

0

2 Answers 2

1

It's throwing the NoSuchElementException because the String you are providing in the instance of the scanner on this line:

Scanner charlie = new Scanner(line);

Is actually just:

Scanner charlie = new Scanner("");

Since you never modified line after you set it to "".

You should be doing the following:

Scanner charlie = new Scanner(System.in);

Also, a Scanner instance doesn't accept a String.

Sign up to request clarification or add additional context in comments.

Comments

0

I needed to define the scanner inside the while loop after line got updated each time, thanks everyone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.