Solved

A simple question from a Java newbie

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 72
Impossible to extract MSI from new JAVA releases 2 47
JavaScript/Java - Changing an image background color 4 54
MySQL  on Tomcat 8 24
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 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