I have a text file with several names (I have listed few names as example):
eden ahbez, American musician
bill bissett, Canadian poet
asha bandele, American writer
danah boyd, American scholar
e e cummings, American poet
mc chris, American rapper
I want to capitalize each first letter of each word or name, including those after the comma. Whenever I try to do it, the output is showing only the first line and discarding the other lines and even it is not capitalizing the letters. How can I read from the same file, capitalize as mentioned, and save the changes in the same file? Because in my case, it is only reading the first line.
I use, for example this code to read the file:
BufferedReader br1 = new BufferedReader(new FileReader("names.txt"));
String phrase = "";
String rline;
while ((rline = br1.readLine()) != null) {
phrase = rline;
char[] line = phrase.toCharArray();
for (r = 0; r < line.length; r++) {
if (line[r] == ' ' && line[r] == ',' && line[r] != '.') {
line[r + 1] = Character.toUpperCase(line[r + 1]);
}
output = Character.toString(line[r]);
System.out.print(output);
}
System.out.println();
}
br1.close();
then this to save the changes:
PrintWriter pw1 = new PrintWriter("names.txt");
pw1.println(output);
pw1.close();
Any suggestion?