Home > Software design >  Make second word capital in a string
Make second word capital in a string

Time:10-28

I don't really know how to explain the problem. I do have a scanner imported after the package. I'm not sure if you can stack methods, and if you can I'm definitely doing it wrong.

    Scanner console = new Scanner(System.in);
    System.out.print("Enter your name: ");
    
    String name = console.next();
    name.trim();
    name.toUpperCase(name.substring(name.charAt(name.indexOf(" "))));
   
    
    
    System.out.println("Your name is: "   name);

CodePudding user response:

Just split the String into the first word and second word based on the indexOf(" ")

Capitalize the second word using toUpperCase

Concatenate both words together using

name = name.substring(0, name.indexOf(" "))   name.substring(name.indexOf(" ")).toUpperCase();

Note: Not sure if you are required to handle invalid input, but this code would only work assuming a valid two-word name is entered, with a space in-between

Also, make sure to change console.next() to console.nextLine() to ensure you retrieve the entire line of input

CodePudding user response:

toUpperCase() expects Locale if a parameter is given. Therefore name.substring(name.charAt(name.indexOf(" "))) cannot be interpreted as Locale.

Strings are immutable, so you have to reassign them after changing name = name.trim();

Usage:

String name = "John Doe";
String[] names = name.split(" ");

name = names[0]   " "   names[1].toUpperCase();

CodePudding user response:

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    System.out.print("Enter your name: ");
    String name = console.nextLine();
    String[] words = name.split(" ");
    words[1] = capitalizeWord(words[1]);
    name = String.join(" ", words);
    System.out.println("Your name is: "   name);
}

private static String capitalizeWord(String s) {
    s = s.substring(0, 1).toUpperCase()   s.substring(1).toLowerCase();
    return s;
}

At first you split the input into a String array. Then you replace the first character of the 2nd (index 1) element and join the array back to a String.

Input: john doe, Output: john Doe

CodePudding user response:

String is immutable in java so if you want to "change" a Strig variable value, you need to reassign it to itself. I think a good approach to prepare more than 2 input, many people has middle name e.g. This function split the input into parts then make the first letter uppercase then return the whole name after concat them with space.

 public String capitalizeName(String name) {
    String[] nameParts = name.split(" ");
    for (int i = 0; i < nameParts.length; i  ) {
        nameParts[i] = nameParts[i].substring(0, 1).toUpperCase()   nameParts[i].substring(1);
    }
    return String.join(" ", nameParts);
}

CodePudding user response:

String is immutable in Java.

BE SIMPLE!!!

public static void main(String... args) throws IOException {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter your first name and last name: ");

    String firstName = upperCaseFirstLetter(scan.next().trim());
    String lastName = upperCaseFirstLetter(scan.next().trim());

    System.out.println("Your name first name: "   firstName);
    System.out.println("Your name last name: "   lastName);
}

private static String upperCaseFirstLetter(String name) {
    return Character.toUpperCase(name.charAt(0))   name.substring(1);
}

CodePudding user response:

One solution would be to make the String a String array using the split(regex)-method. It will split up a string into a String array, breaking them up at regex. For example:

String text = "This is a text.";
String textArray = text.split(" ");

for(String element : textArray)
{
    System.out.println(element);
}

will print

This
is
a
text.

If you got a String[] like that, you can choose the second String (index 1 of array) and capitalize it. You can do so in a foreach loop, for example.

String text = "This is a text.";
text = text.trim(); // if you want to trim it.
String[] textArray = text.split(" ");

String newText = "";
    
int index = 0;
    
for(String element : textArray)
{
    if(index == 1)
    {
        element = element.toUpperCase();
    }
    newText = newText   element   " ";
    index  ;
}
    
System.out.println(newText);

If you want to handle errors, you can put in in a try-catch-block like this.

try
{
    [Your code]
}
catch (Exception e)
{
    System.out.println("An error occured.");
}

This is, of course, not a very short way to do it. However, it's easy to understand and it can handle even a string consisting of several words.

  • Related