0
    String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();

    Scanner l = new Scanner (line);
    int index = line.lastIndexOf(" ");

    while (l.hasNext())
    {
        String name = l.next();
        char ch = name.charAt(0);
        System.out.print(ch);
    }
    System.out.print(" " + line.substring(index + 1));

Example:

User enters the following - Fred John Samuel Smith

The output should be - FJS SMITH

The code doesn't work because the output is this:

FJSS SMITH

How do I split the first 3 words from the last word?

J M
  • 17
  • 4
  • Note: A scanner is not needed for this purpose. Check the [String#split](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) method. Split the string with space, use a `for` to loop the resulting array and use the index to check if you reached the last word. – BackSlash Mar 29 '17 at 11:21
  • And about your question: Possible duplicate of [How to capitalize the first character of each word in a string](http://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string) – BackSlash Mar 29 '17 at 11:21

2 Answers2

1

This may be the solution to your question.

String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();

    String l[] = line.split(" ");
    int index =0;
while(index<4)
{
if(index<3)
 System.out.print(l[index].charAt(0));
else
  System.out.print(" "+l[3]);

index++;
}
asad_hussain
  • 1,959
  • 1
  • 17
  • 27
0

try my approach I have provided the details in the comments

public static void main(String args[])
            {
                //get the first Three Letter acronym and the last name
                StringBuilder sb = new StringBuilder("");
                String line = JOptionPane.showInputDialog("Enter a full name and surname").toUpperCase();
                sb.append(line.charAt(0));
                int spaces=0;
                //get the actual spaces
                for(int i=0;i<line.length();++i)
                {
                    if(line.charAt(i)==' ')
                        ++spaces;
                }

                for(int i=1;i<line.length();++i)
                {
                    //compare each character of inputted String to space
                    if(line.charAt(i)==' '&&spaces>1)
                    {
                        sb.append(line.charAt(i+1));
                        --spaces;
                    }
                    //if space is now equal to one 
                    else if(line.charAt(i)==' '&&spaces==1)
                    {
                        //it will consume all the remaining letter using this loop including the space character
                        while(i<line.length())
                        {
                            sb.append(line.charAt(i));
                            ++i;
                        }
                    }



                }
                //print test of StringBuilder sb
                System.out.println(sb.toString());


            }
0xDEADBEEF
  • 590
  • 1
  • 5
  • 16