0

I currently struggling trying to extract a certain substring of a string and replacing it's content. The digits to be exact. The numbers change, so I can not take the whole string. For example, I have a message like

String message = "hi John. You have two contracts listed: 
                  TV-70002548 and FV-50006578. The first contract 
                  ends on March 15th. I will pay you a final amount of 350 $."

In that String I have a set of different numbers, but I only want to extract the contract numbers and have them rewritten, like TV-70002548 -> TV-seven null null null two five four eight.

I wrote a method that extracts all digits after V- because this character is unique for the contract numbers in a string:

   private String replaceContractNo(String pMessage, String pInput, String pOutput) {
        String contractno;
        contractno = pMessage.substring(pMessage.indexOf("V-")-2);
        return contractno.replaceAll(pInput, pOutput);
    }

Later I want to replace the digits with words, like:

replaceContractNo(pMainMessage,"0"," null")
replaceContractNo(pMainMessage,"1"," one")

This does not work, because the message gets written out each time. Is there an easy solution to this problem?

Thank you

2 Answers 2

1

So this code may be kind of ugly but it works. I'm sure that you can clean it up, but it gets the job done (just make a new file and copy and paste it if you'd like, and if you can't find the better algorithm, let me know, and I'd be happy to help):

public class contract {

  public contract(){

  }
  public static String replaceContractNo(String pMessage, String pInput, String pOutput) {

      return pMessage.replaceAll(pInput, pOutput);
    }

    public static void main(String args[]){

      contract myContract = new contract();

      String message = "hi John. You have two contracts listed: TV-70002548 and FV-50006578. The first contract ends on March 15th. I will pay you a final amount of 350 $.";

      int start1 = message.indexOf("TV");
      System.out.println(start1);
      int end1 = message.indexOf(' ', start1);
      System.out.println(end1);
      String str1 = message.substring(start1, end1);
      System.out.println(str1);

      String test1 = replaceContractNo(str1, "0", "null");
      System.out.println("test1 = "+test1);

      int start2 = message.indexOf("FV");
      int end2 = message.indexOf(' ', start2);
      String str2 = message.substring(start2, end2);

      String test2 = replaceContractNo(str2, "5", "five");
      System.out.println("test2 = " +test2);

      message = message.replaceAll(str1, test1);
      message = message.replaceAll(str2, test2);

      System.out.println(message);

        }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks a lot. I found a solution similar to yours already. But thank you for sharing!
0

Is this what you are trying to do?

str1.replaceAll(oldString, newString);

10 Comments

Hi, yes exactly!
Is policyNumber a global variable? Also, I don't see where you are using contractno at all in this function? I mean contractno is changed in the scope of the function but what is it really used for?
sorry, that was a spelling mistake. It should be contractno
I see. Can you show me what the string you would like to produce would look like?
When there is either TV-70002548 or FV-50006578 (or any other contract number) , I want to store the number "70002548" and "50006578" and rewrite them as "seven null null null two five four eight" etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.