tokeniser

tokeniser

program to find middle name eg rakesh kumar rathod so the output will be kumar .. bt tokeniser

View Answers

June 18, 2012 at 12:00 PM

Here is an example of StringTokenizer class that accept the full name from the user and display the middle name.

import java.util.*;
public class GetMiddleName
{
       public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter your full name: ");
            String fullName = input.nextLine();
            StringTokenizer stok = new StringTokenizer(fullName);
            String firstName = stok.nextToken();

            StringBuilder middleName = new StringBuilder();
            String lastName = stok.nextToken();
            while (stok.hasMoreTokens())
            {
              middleName.append(lastName + " ");
              lastName = stok.nextToken();
            }
            System.out.println("Middle Name: "+middleName.toString().trim());
          }
        }

June 18, 2012 at 12:07 PM

The given code also displays the middle name by accepting the full name from the user.

import java.util.*;
public class GetMiddleName
{
public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter your full name: ");
            String fullName = input.nextLine();
            int firstSpace = fullName.indexOf(" ");
            int lastSpace = fullName.lastIndexOf(" ");
            String middleName = fullName.substring(firstSpace,lastSpace);
            System.out.println("Middle Name: "+middleName);
        }
}









Related Tutorials/Questions & Answers:
tokeniser
tokeniser  program to find middle name eg rakesh kumar rathod so the output will be kumar .. bt tokeniser   Here is an example of StringTokenizer class that accept the full name from the user and display the middle name
java applets - Java Beginners
no. of words in a line of text using string tokeniser... without using swing
Advertisements

Ads