• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting name initials and name length

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to write program that ask for user name (first, middle, last) then prints it out its intials and the length of the first, middle, and last name combined.
I already know how to get user information, but how do I go about doing this?
I know the StringTokenizer has something to do with intials, but how do I write it in code?
Any help or examples would help very much.
Thanks
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I guess you might be using three different textboxes to get the user input (firstname,middlename and surname).
to get the Initials you can use jTextBox1.charAt(0) and similarly for other textboxes.
To get the total length add the length of all the textboxes. (jtextBox1.length()+...)
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String currentName;//contains first mid last name
StringTokenizer st = new StringTokenizer(currentName);
String token = st.nextToken();
char firstInitial = token.charAt(0);
int firstLength = token.length();
token = st.nextToken();
char midIni... etc;
it'd be easier to loop, but that should get you on the right track.
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the program code you are expecting. And you can verify Sun's API for JAVA and you can get the all info regarding.
import java.lang.*;
import java.io.*;
import java.lang.String;
import java.util.*;
import javax.swing.*;
class StringInfo
{
private StringTokenizer name;
private String dum;
private int len=0;
void getInput()
{

String nam=JOptionPane.showInputDialog("Enter Your name");
name=new StringTokenizer(nam) ;
while(name.hasMoreTokens())
{
dum=name.nextToken();
len+=dum.length();
System.out.println(dum);
}
System.out.println("The length of the string without counting spaces is "+ len);
}
}
class StringPro
{
public static void main(String args[])
{
StringInfo inf=new StringInfo();
inf.getInput();
}
}
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For getting Initials u can do what Lalit kumar has said.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic