0

I am trying to search for words within a text file and replace all upper-cased with lower-cased characters. The problem is that when I use the replace All function using a regular expression I get a syntax error. I have tried different tactics, but it doesn't work. Any tips? I think that maybe I should create a replace All method that I would have to invoke, but I don't really see its use.

public static void main() throws FileNotFoundException { ArrayList<String> inputContents = new ArrayList<>(); Scanner inFile = new Scanner(new FileReader("H:\\csc8001\\data.txt")); while(inFile.hasNextLine()) { String line = inFile.nextLine(); inputContents.add(inFile.nextLine()); } inFile.close(); ArrayList<String> dictionary = new ArrayList<>(); for(int i= 0; i <inputContents.size(); i++) { String newLine = inFile.nextLine(); newLine = newLine(i).replaceAll("[^A-Za-z0-9]"); dictionary.add(inFile.nextLine()); } // PrintWriter outFile = // new PrintWriter("H:\\csc8001\\results.txt"); } 
2

1 Answer 1

3

There is a compilation error on this line:

newLine = newLine(i).replaceAll("[^A-Za-z0-9]"); 

Because replaceAll takes 2 parameters: a regex and a replacement. (And because newLine(i) is non-sense.) This should be closer to what you need:

newLine = newLine.replaceAll("[^A-Za-z0-9]+", " "); 

That is, replace non-empty sequences of non-[A-Za-z0-9] characters with a space.

To convert all uppercase letters to lowercase, it's simpler and better to use toLowerCase.

There are many other issues in your code too. For example, some lines in the input will be skipped, due to some inappropriate inFile.nextLine calls. Also, the input file is closed after the first loop, but the second tries to use it, which makes no sense.

With these and a few other issues cleaned up, this should be closer to what you want:

Scanner inFile = new Scanner(new FileReader("H:\\csc8001\\data.txt")); List<String> inputContents = new ArrayList<>(); while (inFile.hasNextLine()) { inputContents.add(inFile.nextLine()); } inFile.close(); List<String> dictionary = new ArrayList<>(); for (String line : inputContents) { dictionary.add(line.replaceAll("[^A-Za-z0-9]+", " ").toLowerCase()); } 

If you want to add words to the dictionary instead of lines, you also need to split the lines on spaces. One simple way to achieve that:

 dictionary.addAll(Arrays.asList(line.replaceAll("[^A-Za-z0-9]+", " ").toLowerCase().split(" "))); 
Sign up to request clarification or add additional context in comments.

8 Comments

so in your opinion, it's better to create another method whose functionnality woud be use used in the main method in order to perform the requested task whether it be to replaceAll or toLowerCase?
I was sightseeing to simply replace replaceAll with toLowerCase. I added more explanation in case it helps, see my update
Thank you so much for your help. I just have one last question, how did you manage to remove all non words from the inputContents list? how to make sure that there are no exclamations marks and other caracters involved?
@Jazztheman ok I actually missed something in your question. I updated my answer, I hope it answers your follow-up question in the comment too.
Hey man, yes, I think you can help me further as my regular expression isn't working and my output is empty. basically the target is to get all words, convert them to lower case, put them in a dictionary on an alphabetical order, one word per line without duplication then output the dictionary in another file. the problem is that the end prdduct is empty. and I don't know why. using your logic, I ended up with the code below
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.