Reputation: 15
I've been learning Java and for some reason, I'm having a brain fart on how to add two strings together. In the program, I successfully have the program state what the length of the First and Last names are independently. However I would like to have the program also state how many characters there are in the name.
I know that I need to assign the string lengths to an integer variable that can be added together, but I'm just blanking at the moment.
My source code is as follows, thank you for your help!
import java.util.Scanner;
/*
* //Program Name: stringwk7
* //Author's Name: MrShiftyEyes
* //Date: 05-12-2013
* //Description: Prompt for a user name; print initials;
* // print out the reverse name, find the length of the names
*/
/**
*
* @author MrShiftyEyes
*/
public class stringwk7{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create first name and last name StringBuffers
Scanner input = new Scanner(System.in);
System.out.print("What is your first name? ");
StringBuffer firstName = new StringBuffer(input.nextLine());
System.out.print("What is your last name? ");
StringBuffer lastName = new StringBuffer(input.nextLine());
// Declare two string variables using StringBuffer variables
String fName = new String(firstName);
String lName = new String(lastName);
// Display the users initials
System.out.println("Your initials are: " + firstName.charAt(0) + " " + lastName.charAt(0));
// Displays the user's name in reverse
System.out.println("Your name in reverse is: " + lastName.reverse() + " " + firstName.reverse());
// Length of name
System.out.println("The length of my first name is " + firstName.length());
System.out.println("The length of my last name is " + lastName.length());
// Insert a goodbye into the firstName string and display to user
firstName = firstName.reverse(); // Change firstName back to the initial value
System.out.println(firstName.insert(0, "See ya later, "));
}
}
Upvotes: 1
Views: 118
Reputation: 760
you just mean the first and last name together?
int fullNameLength = lastName.length() + firstName.length();
System.out.println("The length of my full name is " + fullNameLength);
Upvotes: 3