In: Computer Science
JAVA
1. Write an application that inputs a telephone number as a
string in the form (555) 555-5555. The application should use
String method split to extract the area code as a token, the first
three digits of the phone number as a token and the last four
digits of the phone number as a token. The seven digits of the
phone number should be concatenated into one string. Both the area
code and the phone number should be printed. Remember that you will
have to change deliminator characters during the tokenization
process.
2. Write an application that inputs a line of text, tokenizes the
line with String method split and outputs the tokens in reverse
order. Use space characters as delimiters. For example, input “This
is a beautiful day.” The output should be “day. Beautiful a is
This”
Question
1)
Code Screenshot :
Executable
Code:
import java.util.Scanner;
public class PhoneNumber {
public static void main(String[] args) {
int flag = 0;
String str;
Scanner input = new Scanner(System.in);
System.out.println("Enter the phone number in (xxx)
xxx-xxxx");
str = input.nextLine();
String[] split = str.split("-");
String front = "";
for (int i = 0; i < split[0].length(); i++) {
if (split[0].charAt(i) == '(') {
flag = 1;
continue;
}
if (split[0].charAt(i) == ')')
flag = 0;
if (flag == 1) {
front = front + split[0].charAt(i);
}
}
System.out.println("The final phone number is:" + front +
split[1]);
}
}
Sample Output:
Question
2)
Code Screenshot
:
Executable
Code:
import java.util.Scanner;
class TestCode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
String tokens[] = line.split(" ");
for(int i = tokens.length-1;i>=0;i--){
System.out.print(tokens[i]+" ");
}
}
}
Sample
Output:
Please comment
below if you require any modifications.