0

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?

List of names from Wikipedia

pvg
  • 2,673
  • 4
  • 17
  • 31
Mr. Munoz
  • 69
  • 1
  • 3
  • 13

5 Answers5

2

The logical operator between line[r] == ' ' and line[r] == ',' needs to be a logical OR || rather than a logical AND && because a character can't BOTH.

Also this code will crash if there's a trailing space or a comma since the array won't contain an index of r+1. Lastly && line[r] != '.' does nothing as the other two parts act like a whitelist. If the character is space or comma it will never also be period. Did you mean to make it line[r] == '.'? If so, it also needs a logical OR rather than logical AND.

Tobias
  • 383
  • 2
  • 12
TheAtomicOption
  • 1,456
  • 1
  • 12
  • 21
2

As I see it, you have two errors. First, your if is asking for something impossible (if line[r] is a space, and it's also a comma). It should instead be something like:

if (line[r] == ' ' && r + 1 < line.length) {

Second, you're always overwriting the contents of output with = instead of appending with +=, so that line should read:

output += Character.toString(line[r]);

If you run it like that, you'll notice it's missing something, first letter isn't getting capitalized, so you can put an extra conditional like this:

if (r == 0) {
    line[r] = Character.toUpperCase(line[r]);
}else if (line[r] == ' ' && r + 1 < line.length) {
    line[r + 1] = Character.toUpperCase(line[r + 1]);
}

Here you have a working example

Piyin
  • 1,823
  • 1
  • 16
  • 23
1

The logic to capitalize the initial letters is completely broken. For example, the condition line[r] == ' ' && line[r] == ',' && line[r] != '.' will never be true. Here's one way to fix that:

try (BufferedReader br = new BufferedReader(new FileReader("names.txt"))) {
    String line;

    while ((line = br.readLine()) != null) {
        char[] chars = line.toCharArray();
        for (int r = 0; r < chars.length; r++) {
            if (r == 0 || chars[r - 1] == ' ') {
                chars[r] = Character.toUpperCase(chars[r]);
            }
        }
        System.out.println(new String(chars));
    }
}

I also changed the output mechanism to output the entire completed line instead of printing characters one by one.

janos
  • 120,954
  • 29
  • 226
  • 236
1

As i mentioned above

You override output every time in the loop output = Character.toString(line[r]);, so it contains only 1 line.

Also, your capitalization logic is too complex and broken as you can see from other answers.

So, I made it much easier this way:

    public static void main(String[] args) throws IOException {
        BufferedReader br1 = new BufferedReader(new FileReader("names.txt"));
        String rline;
        String output = "";

        while ((rline = br1.readLine()) != null) {
            String[] lineArr = rline.split(" ");

            for (String word : lineArr) {
                word = Character.toUpperCase(word.charAt(0)) + word.substring(1);
                output += word + " ";
            }

            output += "\n";
        }

        System.out.print(output);

        br1.close();
    }

The Output:

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
Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
1

WordUtils from org.apache.commons.lang implements many methods to manage texts. Here is an exapmple:

public static void main(String[] args) {
        String phrase = "eden ahbez, American musician";
        System.out.println(WordUtils.capitalize(phrase));
}