1

I am writing a parser that removes all punctuation from a text file and puts the words in a Map that associates each word with the number of times it occurs in the file. I use a Scanner to read the txt file, but it reads the file name instead of the actual file. For instance:

    parse("./src/filename.txt")

is read as "srcfilenametxt" and is associated with a value 1. Unfortunately, I can't include more code because this is for a class assignment. How do I get it to correctly read the file?

1
  • Scanner does not have a parse() method. You'll need to show us at least a bit more code. Where is a Scanner involved here? Commented Mar 31, 2013 at 18:43

2 Answers 2

2

If Scanner is constructed with a string parameter it scans the string, not the file named by the string. You'd need a line like:

Scanner in = new Scanner(new File("./src/filename.txt"));
Sign up to request clarification or add additional context in comments.

1 Comment

@AlexHaldeman: Glad to help. If you would like to tick this answer as having met your needs, that will save other people coming to your question and suggesting other answers.
0

Use bufferedreader to read a file

BufferedReader br = new BufferedReader(new FileReader("filename.txt"));

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.