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