1

Here is my code: import java.util.Scanner;

class namedisplay {
public static void main(String args[]){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your name: ");
    String name = input.nextLine();

    String capital1 = name.substring(0).toUpperCase();
    String capital2 = name.substring(5).toUpperCase();

    System.out.println(capital1+capital2);



}
}

The program output: Enter your name: anna lee ANNA LEELEE

What I want the program to do is to capitalize only the first letters of the first name and last name, for example, Anna Lee.

name123
  • 773
  • 3
  • 9
  • 9

1 Answers1

1
System.out.println("Enter your name: ");
String name = input.nextLine(); 

String newName = "";

newName += name.charAt(0).toUpperCase();
newName += name.substring(1, name.length());

System.out.println(newName);

To get the first letter and capitalize, you use this name.charAt(0).toUpperCase();. Then add that to the newName.

Then you want to add the remaining letters from name to newName. You do that by adding a substring of name

name.substring(1, name.length());  // 1 mean the substring will start at the 
                                   // second letter and name.length means the 
                                   // substring ends with the last letter
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720