Question: Extract last name from user input. Once a user input his/her full name, then use split() method. Split() is a method in String class. Find

Extract last name from user input.

Once a user input his/her full name, then use split() method. Split() is a method in String class. Find the split() in JAVA API to get the syntax and how to use it. (20)

Input Index number of the string

We studied the String class and object already. Remember that string index number starts with 0. Once a user input starting and ending index numbers for the lastname, use substring() to take out last name. (20)

Compare two strings

One string (=last name) extracted using split() and the other(=last name) is extracted using substring(). Compare it whether it is the same or not. (20)

Exception handling (try, catch)

If a user input not correct index number, but it is in the size of the string, then ask a user to input new index number (must use a loop to ask new index number). If index number input is out of the size of the string, then throws exception. In this case, it must display a message (default message) to the user. Find the correct exception type. We already studied the exception type for this case in the classroom. (30)

Display the result on the screen

Your result should be similar to following: (5)

Sample Run 1:

Enter your full name: Andrew Jung

Andrew Jung

Enter the beginning and ending index number of your lastname: 0 7

Not correct number for extracting your last name.

Enter the beginning and ending index number of your lastname: 7 11

Last Name: Jung

Sample Run 2:

Enter your full name: Andrew Jung

Andrew Jung

Enter the beginning and ending index number of your lastname: 0 7

Not correct number for extracting your last name.

Enter the beginning and ending index number of your lastname: 55 55

Not correct number for extracting. String index out of range: 55

Sample Run 3:

Enter your full name: Andrew Jung

Andrew Jung

Enter the beginning and ending index number of your lastname: 0 55

Not correct number for extracting. String index out of range: 55

mycode :

import java.io.*;

import java.util.Scanner;

import java.util.*;

public class Quan_lab4

{

public static void main(String [] args)

{

String Name;

int space;

String spiltname;

String lastname;

int a=0;

int b=0;

boolean A =true;

Scanner keyboard = new Scanner(System.in); //make scanner name

System.out.print("Enter your full name: ");

Name = keyboard.nextLine();

space = Name.indexOf(' ');

String[] arrofName = Name.split(" "); //the split methods would spilt the space between the name.

spiltname = Name.split(" ")[1];

lastname = Name.substring(a,b);

while (A = true)

{

try

{

System.out.print("Enter the beginning and ending index number of your lastname: ");

a = keyboard.nextInt();

b = keyboard.nextInt();

lastname = Name.substring(a,b);

if (lastname == spiltname)

break ;

System.out.print("Not correct number for extracting your last name.");

System.out.print("Enter the beginning and ending index number of your lastname:");

a = keyboard.nextInt();

b = keyboard.nextInt();

}

catch (StringIndexOutOfBoundsException exception)

{

System.out.println("Not correct number for extracting. String index out of range: "+ b);

}

}

}}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!