Question: write a program which prompts the user to enter their full name as a single String. The program will then tell the user how many letters are in their first name, and will display their initials. ( their second initial will be the letter immediately after the space)
My code:
// Lecture 1 Classes and Objects
// gman
// 29/01/2012
import java.util.Scanner;
public class Q7NameCountInitals
{
public static void main(String []args)
{
Scanner kb = new Scanner(System.in);
// declare string
String name = new String();
int firstChar =0;
// get user input
System.out.println("Enter your name ");
name = kb.nextLine();
char fNc = name.charAt(0);
int pos = name.indexOf(" ");
char sNc = name.charAt(pos + 1);
for (int i = 0; i < pos; i++)
{
firstChar++;
}
System.out.println("your first name has " + firstChar + " characters." );
System.out.println("your initials are " + fNc + "" + sNc);
}
}
Thanks in Advance:
Gman


Sign In
Create Account


Back to top









