import java.util.*;

    public class Initials
        {
            public static void main (String [] args)
            {
            Scanner keyboard = new Scanner (System.in);
            String name;


            System.out.println("Please enter your full name including your middle name: ");
            name = keyboard.nextLine();
            System.out.println(" ");

int x = name.indexOf(' ')+1;
String nameTwo = name.substring(x,name.length());
int y = nameTwo.indexOf(' ')+1;
System.out.println(name +" , your initials maybe this: " + name.charAt(0) + name.charAt(x) + name.charAt(y));



        }
    }

this is what i have and this is what it is supposed to do but it prints out the second intial twice.. where did i go wrong

Write a program that inputs a user’s full name with spaces between the first, middle,
and last names. The program should output the user’s initials. You may not use any
looping structure in this program. Hint you can do this quite easily with the String
method indexOf(). Save the program as Initials.java. Good luck.
Test Data
Input your name with one space between each name: John Quincy Adams
John Quincy Adam, your initials are: JQA
Input your name with one space between each name: Vicki Renee Williams
Vicki Renee Williams, your initials are: VRW
Input your name with one space between each name: Elvis Aaron Presley
Elvis Aaron Presley, your initials are: EAP

Because you are searching for index of character in a substring, not in the main (name) string. Simply remove line 16 (nameTwo) and utilize the indexOf() method (hint instead of using indexOf(int ch), use indexOf(int ch, int fromIndex)).

What I would do instead is something like this pseudo code

input from user // such as(name name1 name2)
split the input from user and your regex is space, into an array of Strings
at this point you have 3 strings into an array, your initials are the first char
for each of those strings 

String name = input.scanner(System.in);
String[] names = name.split(" ");
for (String s : names) System.out.print(s.chatAt(0));

Slavi, the requirement is "You may not use any
looping structure in this program." :P

Taywin: yes, but even with indexOf, as suggested by the teacher himself, there is either some looping involved, or it won't work.

I will work if the OP does correctly because the requirement assumes that the input format is always correct. :) indexOf() will work if the OP doesn't chop the string as of now.

yes. but: some people don't have a middle name. some have two.
for a good solution, either he needs to loop, or it's a runtime exception waiting to happen.

As I mentioned, the program is simply for practice; thus, the input format will always be first, middle, and last. If the instructor wants that tricky part, then he/she should mention to use the loop. Besides, it is not a good way of teaching to say one thing but omit others just to make it incorrect. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.