0

With for loop and if else statement I have already changed the initial character of words to upper case.

I would like to know how to store the value into the x variable so that it can return to main and print it out.

The output suppose to be "The Simpson Series" instead of "the simpson series". enter image description here

Thank you.

package tt5;

public class StringCharacter {

public static String capitalize(String x){

//check character length
for(int i=0; i<x.length(); i++){
   Character temp = x.charAt(i);
   boolean b1 = Character.isLetter(temp);

//change words to uppercase if condition matched
    if(i==0){
        String y = temp.toString();
        String a = y.toUpperCase();
    }
    else if(b1==false){
            temp = x.charAt(i+1);
            String y = temp.toString();
            String a = " " + y.toUpperCase();
            i++;
    }
    else{
            String a = temp.toString();
    }
}

//return result
String a = "";
x = x.concat(a);
return x;
}

//input words and print the result
public static void main(String[] args){
    String str = "the simpson series";
    String total = capitalize(str);
    System.out.println(total);
}

}

Yap Wayne
  • 19
  • 5
  • how can your question be tagged javascript and java ? And you just need to declare your String a before the for loop (don't assign "" to it after the loop) – Paul Lemarchand Oct 08 '17 at 12:01
  • What is your code supposed to do? And what is not working? – M. le Rutte Oct 08 '17 at 12:09
  • In case you want to start each word inside the string with a capital letter, see this question: https://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string – deHaar Oct 08 '17 at 12:17

3 Answers3

2

Solution 1: StringBuilder

public class Capitalize {
    public static String capitalize(String string) {
        StringBuilder builder = new StringBuilder(string.length());

        boolean capitalizeNext = true;
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);

            if (capitalizeNext && Character.isLetter(c)) {
                c = Character.toUpperCase(c);
                capitalizeNext = false;
            } else if (Character.isWhitespace(c)) {
                capitalizeNext = true;
            }

            builder.append(c);
        }
        return builder.toString();
    }

    //input words and print the result
    public static void main(String[] args){
        String str = "the simpson series";
        String total = capitalize(str);
        System.out.println(total);
    }   
}

Solution 2: streams

import java.util.Arrays;
import java.util.stream.Collectors;

public class Capitalize {
    public static String stream(String text) {
        return Arrays.stream(text.split("\\s+"))
            .map(s -> s.substring(0,1).toUpperCase() + s.substring(1))
            .collect(Collectors.joining(" "));
    }

    public static void main(String...string) {
        System.out.println(stream("the simpson series"));
    }
}

(although, technically, it skips multiple whitespaces into a single space)

M. le Rutte
  • 3,525
  • 3
  • 18
  • 31
1
import java.util.*;
import java.lang.*;
import java.io.*;

public class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {

    String s ="the anupam singh";
    s=test(s);
    System.out.println(s);

    }


    public static String test(String s)
    {
        char c;

        for(int i=0;i<s.length();i++)
        {
          c=s.charAt(i);

          if(i==0)
          {
            c=Character.toUpperCase(c);
            s=c+s.substring(1,s.length());
          }
          else
          {
            if(c==' ')
            {
                c=s.charAt(i+1);
                c=Character.toUpperCase(c);
                s=s.substring(0,i+1)+c+s.substring(i+2,s.length());
            }

          }

        }

        return s;


    }


}

I hope you were trying to change the initial character of every word into uppercase character. I have written code for that. It's working.

Coming to your question of storing it into x variable. make string x external and then you can access it from both the methods i.e main and capitalize.

anupam691997
  • 310
  • 2
  • 8
  • What happens with multiple spaces between words in your code? What happens if the string ends with a space? – M. le Rutte Oct 08 '17 at 12:46
  • i didn't think about the corner cases. I just gave the user a basic idea on how to do this. these corner cases can be handled easily. – anupam691997 Oct 08 '17 at 12:56
0

split each word of the given line of String by \\s+ (space) and later capitalize first character of each word and append them using StrinBuilder

public static String toTitleCase(String givenString) {
    String[] arr = givenString.split("\\s+");
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < arr.length; i++) {
        sb.append(Character.toUpperCase(arr[i].charAt(0)))
            .append(arr[i].substring(1)).append(" ");
    }          
    return sb.toString().trim();
}
Ravi
  • 30,829
  • 42
  • 119
  • 173