So i did my code, he asks for the string then i put it, then he tells me what string i put in but for some reason i dont understand he doesnt show the capitalized first letter for each word coverted string.

Here is my code:

import java.io.*;
public class LerString {
public static void main(String[] args) {
boolean stop;
do {
System.out.println("Enter your string");
BufferedReader si = new BufferedReader(
new InputStreamReader(System.in));
String s = null;
try { s = si.readLine(); }
catch(IOException e) { System.out.println("error"); }
System.out.println("You Entered " +s);


stop = s.equals("stop it");
}
while(!stop);


}


static void printCapitalized(String s) {
char ch;
char prevCh;
prevCh = '.';
for ( int i=0 ;  i < s.length();  i++ ) {
ch = s.charAt(i);
if ( Character.isLetter(ch)  &&  ! Character.isLetter(prevCh) )
System.out.print( Character.toUpperCase(ch) );
else
System.out.print( Character.toLowerCase(ch) );
prevCh = ch;
}
System.out.println();
}



}

Any Ideas would be much appreciated

Cheers

Dani AI

Generated

Short version: the capitalizer itself is fine, but you never call it from main. pointed that out and is right that the program does exactly what you asked. Add a call to your routine after you read the line and it will print the transformed string.

Example of a safe loop (move the reader out of the loop and avoid NullPointerException):

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
do {
    System.out.print("Enter your string: ");
    s = br.readLine();
    if (s == null) break;                        // EOF or read error
    System.out.println("You Entered " + s);
    printCapitalized(s);                         // <<-- call the method here
} while (!"stop it".equalsIgnoreCase(s.trim()));

A few practical improvements to consider:

  • Use "stop it".equalsIgnoreCase(s.trim()) so an accidental null or different case/whitespace won't crash the program.
  • Create the BufferedReader once (as shown) and close it when done (or use try-with-resources in Java 7+).
  • Make the capitalizer return a String instead of printing directly. Returning a string makes testing and reuse easier:
    String result = capitalizeWords(s);
    System.out.println(result);
  • The char-by-char approach will uppercase letters after an apostrophe (e.g., "don't" -> "Don'T"). A simple alternative is to split on whitespace and capitalize each token’s first letter while lowercasing the rest; that preserves contractions correctly. For full locale-aware behavior consider BreakIterator or a library like Apache Commons Lang if needed.

Recommended Answers

All 2 Replies

Well, you do not call the printCapitalized() method anywhere in your main method, so it's not going to do anything until you call it.

jup, nothing wrong with the code, and it's doing all you've told it to do.

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.