Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A simple question from a Java newbie

Posted on 2008-10-08
5
Medium Priority
?
203 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …
Suggested Courses

916 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