Make second word capital in a string

1.1k Views Asked by At

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);
5

There are 5 best solutions below

0
mmartinez04 On BEST ANSWER

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

0
Oleg Cherednik On

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);
}
0
Christoph S. On
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

0
Zero On

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.

0
Chezo On

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);
}