Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

A simple question from a Java newbie

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
Groovy problem when using SOAPUI : DispatchException occurred 7 58
varialbe initialization 11 37
web application structure 18 100
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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…

861 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