Question

Help parsing strings in java, regex can be used.

Mask all the digits in a phone number except the last 4 dligits. Each number should be replaced by stan)Input Phone numbers can be with or without country code. Input Phone numbers can only haveorin them along with numbers and spaces. Make sure- is not masked in the output and make sure the number of stars is equal to the number of digits while masking. Phone numbers will always have 10 digits without country code and with country code they could be up to 13 digits. Example 1: +1 (333) 444-5678-> -.“a_6A,-5678 Example1:+91 (333)141-561 --56B Example! : +1 1 1 (333) 444-5678 -.> ÷ s_em.xxx-5678 Example2: 333 444 557a -x-5676 Example: (333) 444-56785678

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN JAVA:

import java.util.Scanner;
import java.io.*;
public class Conversion {
    public static void main(String args[]){
         Scanner sc=new Scanner(System.in);
        String phone;
        System.out.print("Enter a phone number:");
        phone=sc.nextLine();
        StringBuilder str1=new StringBuilder(phone);
        int len,i,count=0;
        len=str1.length();
        for(i=len-1;i>=0;i--){
            if(Character.isDigit(str1.charAt(i))){
                count++;
                if(count>4){
                    str1.setCharAt(i, '*');
                }
            }
        }
        System.out.println(str1);
    }
}

OUTPUT:

етап :: Enter a phone number:1 (333) 444-5678 +* (***) ***-5678 BUILD SUCCESSFUL (total time: 26 seconds)run: Enter a phone number:+111 (333) 4444-5678 +***(***)****-5678 BUILD SUCCESSFUL (total time: 27 seconds)

Add a comment
Know the answer?
Add Answer to:
Help parsing strings in java, regex can be used. Mask all the digits in a phone...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Supposing we are all celebrities who care about our privacy as much as our programming course...

    Supposing we are all celebrities who care about our privacy as much as our programming course grades....we hope our email address and cell phone number are masked if we type them online. Now you need to write a python/java code to realize this masking function. Requirement: (1) Assuming the input email address is in the format of "[email protected]", all names must be converted to lowercase and all letters between the first and last letter of the name1 must be replaced...

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • Need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after ...

    need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after the decimal point. Process and sample run: a) Function 1: A void function that uses parameters passed by reference representing the name of a data file to read data from, amount of principal in an investment portfolio, interest rate (any number such as 5.5 for 5.5% interest rate, etc.) , number of times the interest is compounded, and the number of years the money is...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack)...

    **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (last-in first-out) protocol. # # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. def create(): """ Purpose creates an empty stack Return an empty stack """ return '__Stack__',list() def is_empty(stack): """...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT