public static String strCap(String str){

//capitalizes the first letter of the string		
String newStr1 = str.replace(str.charAt(0), Character.toUpperCase(str.charAt(0))); 

int position = str.indexOf(". "); // looks for a period followed by a space
	
while(position != -1){   
            
newStr1 = newStr1.replace(newStr1.charAt(position + 2), Character.toUpperCase(newStr1.charAt(position + 2)));
			
position = str.indexOf(". ", position+1);
			
		
}
		
return newStr1;
		
	
}

the user types in a string and the program capitalizes where necessary. the first letter of the sentence is taken care of, but the rest i think it's perfect, but it's not working. i can't figure out what's wrong with this method i have here. it gives me weird results sometimes like this: DSf DSf. SDf SDf.

ok now i know what i did wrong, so is there a java command that replaces only 1 occurrences of character found instead of replacing them all?

No, we don't have any method in String class to replace one occurence of character. But here you need to use the char array and then replace the character at index -> position+2 so that exact occurence will be capitalised instead of all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.