-3

the program is written for converting every first letter of the word to UpperCase

public class MainClass {

    public static void main(String[] args) {

        int i;
        String toBeCapped="";

        String str[] ={"This is a ball","This is a bat","this is the wicket"};
        int e=str.length;

        for(int j=0;j<e;j++)
        {
             String[] tokens = str[j].split("\\s");

             for( i = 0; i < tokens.length; i++)
             {
                 char capLetter = Character.toUpperCase(tokens[i].charAt(0));

                 toBeCapped +=  " " + capLetter + tokens[i].substring(1);
            }
            System.out.println(toBeCapped);
        }
    }
}

The output produced is as:-

This Is The Ball
This Is The Ball This Is The Bat
This Is The Ball This Is The Bat This Is The Wicket

I wanted the output to be as:-

This Is The Ball
This Is The 
This Is The Wicket

Please tell me what is the mistake I'm making. Thank You

doublesharp
  • 26,888
  • 6
  • 52
  • 73

3 Answers3

3

The problem is that you never reset toBecapped to "" in the loop after printing.

Adding toBeCapped="" at the end of the loop after printing will fix this problem:

System.out.println(toBeCapped);
toBeCapped=""; // <<== Add this line

Note that string concatenation is relatively expensive in Java. A better approach is to use StringBuilder. See this Q&A for an in-depth discussion of this topic.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Also a good idea to use a [`StringBuffer`](https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html) if you are concatenating Strings. – doublesharp Oct 10 '16 at 20:50
  • `StringBuffer` is always a good idea to concatenate `Strings` as it gives less time complexity than the normal `String Concatenation.` – Sanket Makani Oct 10 '16 at 21:02
  • Thank You bro.Also I would like to ask you that how I can create this program using StringBuffer? – Aradhya Jain Oct 10 '16 at 21:47
0

Below is the problematic code. The += appends the characters to an already built toBeCapped String. You need to set the toBeCapped String to an empty one after the innermost for loop

for( i = 0; i < tokens.length; i++)
{
  char capLetter = Character.toUpperCase(tokens[i].charAt(0));

  toBeCapped +=  " " + capLetter + tokens[i].substring(1);
}
System.out.println(toBeCapped);
}
toBeCapped = "";
Mechkov
  • 4,294
  • 1
  • 17
  • 25
0

Your code has a problem because toBeCapped variable is never reset after being used in the loop. A good coding practice is to declare and use loop variables inside the loop (not outside of it)

public static void main(String[] args) {
  String str[] ={"This is a ball","This is a bat","this is the wicket"};
  int e = str.length;

  for(int j=0;j<e;j++) {
    String toBeCapped="";
    String[] tokens = str[j].split("\\s");

    for(int i = 0; i < tokens.length; i++) {
      char capLetter = Character.toUpperCase(tokens[i].charAt(0));
      toBeCapped +=  " " + capLetter + tokens[i].substring(1);
    }
    System.out.println(toBeCapped);
  }
}
MaxZoom
  • 7,619
  • 5
  • 28
  • 44