Solved

A simple question from a Java newbie

Posted on 2008-10-08
5
196 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

813 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

12 Experts available now in Live!

Get 1:1 Help Now