-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNameParser.java
More file actions
executable file
·29 lines (25 loc) · 925 Bytes
/
NameParser.java
File metadata and controls
executable file
·29 lines (25 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* NameParser takes a single String-valued command line argument of
* the form "last_name, first_name" and prints the first and last names
* separately.
*/
public class NameParser {
public static String extractLastName(String name) {
int commaPos = name.indexOf(",");
String lastName = name.substring(0, commaPos).trim();
return lastName;
}
public static String extractFirstName(String name) {
int commaPos = name.indexOf(",");
int len = name.length();
String firstName = name.substring(commaPos + 1, len).trim();
return firstName;
}
public static void main(String[] args) {
String fullName = args[0];
String lastName = extractLastName(fullName);
String firstName = extractFirstName(fullName);
System.out.println("First name:\t" + firstName);
System.out.println("Last name:\t" + lastName);
}
}