• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting name initials and name length  RSS feed

 
Nickesha Lindsay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Lalit K Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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()+...)
 
Tony Fabeets
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For getting Initials u can do what Lalit kumar has said.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!