0

Hey guys so im new to java and im trying to write a program that declares two strings First && Last name ( both under case ) And i need to use the .uppercase to convert the first letter in both the first name and last name from lower case to uppercase.

Example convert jon to Jon

This is what i have so far I really dont understand how i make the first letter uppercase.

/////
public class firstProgram {
public static void main(String args[])
{
//NAME GOES HERE. DECLARED 2 STRINGS
String first = "firstname";
String last = "lastname";

//PRINT OUT STRINGS
System.out.println(first);
System.out.println(last);
}
}
jayZ
  • 1

4 Answers4

0

You can do something like -

    String first = "firstname";
    String last = "lastname";
    first = String.valueOf(first.charAt(0)).toUpperCase() + first.substring(1);
    last = String.valueOf(last.charAt(0)).toUpperCase() + last.substring(1);
    //PRINT OUT STRINGS
    System.out.println(first);
    System.out.println(last);

You can check documentation for toUpperCase().

Aniket Thakur
  • 58,991
  • 35
  • 252
  • 267
0

If you only want to capitalize the first letter of a string named first and leave the rest alone:

first = first.substring(0, 1).toUpperCase() + first.substring(1);

Now first will have what you want. Do like this for last

0

You can do this by employing the String class subString methods.

String input = "first name";
String out = input.substring(0, 1).toUpperCase() + input.substring(1);
Hanzallah Afgan
  • 696
  • 5
  • 23
0
firstName = Character.toUpperCase(firstName.charAt(0)) + firstName.substring(1);

lastName = Character.toUpperCase(lastName.charAt(0)) + lastName.substring(1);

or in class

class FirstProgram {

    public static void main(String[] args) {
        String firstName = "arun";
        String lastName = "kumar";
        firstName = Character.toUpperCase(firstName.charAt(0)) + firstName.substring(1);
        lastName = Character.toUpperCase(lastName.charAt(0)) + lastName.substring(1);
        System.out.println(firstName+ " "+lastName);    
    }
}
Arun Kumar
  • 2,734
  • 1
  • 15
  • 15