My teacher wants us to create the following program:
Ask user to enter: First Name, Middle Name, Last Name
Output: FName_Initial MName_Initial, Last Name
And Length of the Input string.
Example:
User Input: Helen Jane Brown
Output: String Length: 16 Characters
User Name: H J Brown
The teacher wants us to use indexOf to show the length of the output once the user inputs their entire name...i have no clue how to do that, or how to show the adding up of the characters since each input is typed in differently, meaning. the user will input first name, then middle name, then last name; so how do u create a string or int to add up 3 different strings?
im soooo lost....below is wut i came up with, but of course it has a lot of errors. thanks for any help
import java.util.*;
public class FMLast
{
static Scanner console = new Scanner(System.in);
public static void main (String[] args)
{
String lastName = "";
String middleName = "";
String firstName = "";
int index1;
int index2;
int index3;
int length;
index1 = firstName.indexof(" ");
index2 = lastName.indexof(" ");
index3 = middleName.indexof(" ");
length = index1 + index2 + index3;
System.out.print("Enter your first name: ");
firstName = console.next();
System.out.print("Enter your middle name: ");
middleName = console.next();
System.out.print("Enter your last name: ");
lastName = console.next();
System.out.println("User Output: " + firstName + " " + middleName + " " + lastName);
System.out.println("Output: String Length: " + length + " Characters");
System.out.println("User Name: " + firstName.charAt(0) + " " + middleName.charAt(0) + " " + lastName);
}
}
Ask user to enter: First Name, Middle Name, Last Name
Output: FName_Initial MName_Initial, Last Name
And Length of the Input string.
Example:
User Input: Helen Jane Brown
Output: String Length: 16 Characters
User Name: H J Brown
The teacher wants us to use indexOf to show the length of the output once the user inputs their entire name...i have no clue how to do that, or how to show the adding up of the characters since each input is typed in differently, meaning. the user will input first name, then middle name, then last name; so how do u create a string or int to add up 3 different strings?
im soooo lost....below is wut i came up with, but of course it has a lot of errors. thanks for any help
import java.util.*;
public class FMLast
{
static Scanner console = new Scanner(System.in);
public static void main (String[] args)
{
String lastName = "";
String middleName = "";
String firstName = "";
int index1;
int index2;
int index3;
int length;
index1 = firstName.indexof(" ");
index2 = lastName.indexof(" ");
index3 = middleName.indexof(" ");
length = index1 + index2 + index3;
System.out.print("Enter your first name: ");
firstName = console.next();
System.out.print("Enter your middle name: ");
middleName = console.next();
System.out.print("Enter your last name: ");
lastName = console.next();
System.out.println("User Output: " + firstName + " " + middleName + " " + lastName);
System.out.println("Output: String Length: " + length + " Characters");
System.out.println("User Name: " + firstName.charAt(0) + " " + middleName.charAt(0) + " " + lastName);
}
}