Solved

A simple question from a Java newbie

Posted on 2008-10-08
5
193 Views
Last Modified: 2013-11-23
Hi there,

I have just started learning to write Java and I'm stumped already!

I have to write a short program that asks the user to type their name in full.  The assumption is that their full name comprises of 3 names.  The first initial of each name should then be displayed.

eg, 'Please enter your full name' ~: Flo The Minx
F T M

 The tutorial that I am working from tells me to use the string methods to work out where the first and last spaces are.

Here is what I have so far... (I know it's wrong because the answer it displays is '123')

Also, I brief explanation of the answer would be appreciated as I am trying to learn and I won't learn without knowing how it works, thanks


//Print initials
 

import java.util.*;
 

public class Initials

{

  public static void main (String[]args)

  {

    //create Scanner to read from keyboard

    

   Scanner myKeyboard = new Scanner(System.in);

   

   //ask for name

   System.out.print("Please enter your full name~: ");

   String name = myKeyboard.nextLine();

   

   System.out.print((name.charAt(0)) + (name.indexOf(' ') + 1));

   }

 }

Open in new window

0
Comment
Question by:flo_the_lil_minx
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
contactkarthi earned 500 total points
ID: 22670624
the print statement should be

 System.out.println((name.charAt(0)) +" " + name.charAt(name.indexOf(' ') + 1) + " " + name.charAt(name.lastIndexOf(' ') + 1));
 
0
 
LVL 1

Author Closing Comment

by:flo_the_lil_minx
ID: 31504311
Thanks for that, I'm glad to see I wasn't that far off (ish)
0
 
LVL 1

Author Comment

by:flo_the_lil_minx
ID: 22670735
So I guess what the code is saying in simplistic terms..

System.out.println((name.CharAt(0))
print out the first character of the String name then

name.charAt(name.indexOf(' ') + 1)
print out the character that is one more than the first space

and finally print out the character that is one more than the last space?
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 22670848
this print statement ssumes that there are 2 blank spaces..

as you sait chatAt(0) prints the first character

name.charAt(name.indexOf(' ') + 1) will print the character next to the first space

name.lastIndexOf(' ') + 1) will print the character after the last space..


0
 
LVL 1

Author Comment

by:flo_the_lil_minx
ID: 22670901
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now