Question

I'm having trouble figuring out how to tell if there's whitespace in this program. The program is supposed to take a string, change it to char data, and then capitalize the first letter of every word.

The code is the following:

   import java.util.*;
public class wrapper1
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String s1;
        System.out.println("Enter a phrase: ");
        s1=input.nextLine();
        s1=s1.trim();
        int counter;
        char c;
        boolean flag=true;

        for(counter=0; counter < s1.length(); counter++)
        {
            c=s1.charAt(counter);
            while(counter < s1.length())
            {
                if(flag==true)
                {
                    Character.toUpperCase(c);
                    flag=false;
                }
                else if(!Character.isLetter(c))
                {
                    flag=true;
                }
            }
            System.out.print(c);
        }
    }
}

I made a loop and the goal of that loop is to go through the data capitalizing the first letter of every word by setting the flag to false, and then back to true whenever it finds a whitespace.

However, I do not know how to write the else if statement to check for whitespace, therefore that is what I'm looking for (a way to write that statement).

Any help is appreciated.

Was it helpful?

Solution

To correct your code remove the while statement and assign the result of toUpperCase to c as mentioned by ZouZou

public static void main (String[] args) throws java.lang.Exception
{
    Scanner input = new Scanner(System.in);
    String s1;
    System.out.println("Enter a phrase: ");
    s1=input.nextLine();
    s1=s1.trim();
    int counter;
    char c;
    boolean flag=true;

    for(counter=0; counter < s1.length(); counter++)
    {
        c=s1.charAt(counter);
        if(flag==true)
        {
            c = Character.toUpperCase(c);
            flag=false;
        }
        else if(s1.charAt(counter)==' ')
        {
            flag=true;
        }
        System.out.print(c);
    }
}

Note that your solution only treats spaces as separator characters not other whitespaces (such as tabs) or a sequence of whitespace characters.

Here is a more concise solution that treats tabs and sequences

for(String s : s1.split("\\s+")){
    System.out.print(Character.toUpperCase(s.charAt(0)) + s.substring(1) + " ");
}

And an even more concise version would be to use Apache Commons WordUtils.capitalize

OTHER TIPS

Why do you want to use "charAt" (your code is invalid there, afaikt). You already have the current char at hand. So check if it is a whitespace. You only have to consider consecutive whitespaces - something like if(flag==true && c != ' ')

Additionally, why do it that way? Why not use String.split(...) ?

P.S. Convention says we use Class-names starting upper-case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top