0

I'm making a medicine expiry date reminder project by using zxing library. I'm taking the qr code, trying to get expiry date after space character and transform it to date format. e.g. qr code = "010869971701010921104197131 1722022810170666" > (17)220228 > yyMMdd to ddMMyyyy > result: 28.02.2022

public void handleResult(Result result) {
      String resultCode = result.getText();

      String[] date1 = resultCode.split("\\s");   
      String s = date1[1].substring(2,8);

      SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
      try {           
        Date d1 = sdf.parse(s);;
        sdf.applyPattern("dd.MM.yyyy");
        MainActivity.date.setText(sdf.format(d1));
      }catch(ParseException e) {
        e.printStackTrace();
      }

App is getting crash with this code. If I put value to resultCode manually, app is working. e.g. resultCode = "010869971701010921104197131 1722022810170666" > 28.02.2022

I noticed that the problem is in this line: String[] date1 = resultCode.split("\s");

Then I tried to replace all space character([android splitting with space not working for this case. Why?):

resultCode = resultCode.replaceAll("\\t", "a");
resultCode = resultCode.replaceAll("\\xA0", "b");
resultCode = resultCode.replaceAll("\\u1680", "c");
resultCode = resultCode.replaceAll("\\u180e", "d");
resultCode = resultCode.replaceAll("\\u2000", "e");
resultCode = resultCode.replaceAll("\\u200a", "f");
resultCode = resultCode.replaceAll("\\u202f", "g");
resultCode = resultCode.replaceAll("\\u205f", "h");
resultCode = resultCode.replaceAll("\\u3000", "i");
>>>resultCode = resultCode.replaceAll("(^\\h*)|(\\h*$)","j");
resultCode = resultCode.replaceAll("\\u00A0","k");
resultCode = resultCode.replaceAll("\\u2007","l");

result: "j010869971701010921104197131 1722022810170666j"

Only worked the line that I marked with ">>>". Isn't that a space character? What may be the problem? How can I get expiry date after the blank and "17"?

Although I defined resultCode as a string, its type could be different?

String resultCode = result.getText();

resultCode is getting the data directly from QR code scanner.

FrecoB
  • 1
  • 2

1 Answers1

0

I think you don't need to replace the whitespace.

Your problem is, that the line you marked is for Java 8, which android currently only partially supports.

resultCode = resultCode.replaceAll("(^\\h*)|(\\h*$)","j");

In your case, it is interpreted as "Replace every (0 or more) 'h' character at the beginning and the end with 'j'" (the old Java 7 way).

To get the part you are looking for, try using resultCode.split("\\s17"). It will split the string into the part before the " 17" and after. Then you can simply parse the second array entry, which would be your date.

Your working code should then look like this:

public void handleResult(Result result) {
    String resultCode = result.getText();

    String[] date1 = resultCode.split("\\s17");

    SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
    try {           
        Date d1 = sdf.parse(date1[1]);
        sdf.applyPattern("dd.MM.yyyy");
        MainActivity.date.setText(sdf.format(d1));
    }catch(ParseException e) {
        e.printStackTrace();
    }
}

In addition, this Block doesn't remove your whitespace, but instead replaces it with characters a, b, c, ... Just remove it, you won't need it.

resultCode = resultCode.replaceAll("\\t", "a");
resultCode = resultCode.replaceAll("\\xA0", "b");
resultCode = resultCode.replaceAll("\\u1680", "c");
resultCode = resultCode.replaceAll("\\u180e", "d");
resultCode = resultCode.replaceAll("\\u2000", "e");
resultCode = resultCode.replaceAll("\\u200a", "f");
resultCode = resultCode.replaceAll("\\u202f", "g");
resultCode = resultCode.replaceAll("\\u205f", "h");
resultCode = resultCode.replaceAll("\\u3000", "i");
vstollen
  • 151
  • 1
  • 11
  • Thanks for your reply but it still doesn't work. I used these replace functions for just finding the problem. Is it space or not, and see which character will replace with space character. and I noticed that it isn't space char. Because If I define resultCode manually, it works. resultCode.split("\\s17") is a better split than mine. But I have to use subsplit anyway. I defined resultCode as a string but maybe its type isn't string when this code worked: String resultCode = result.getText(); – FrecoB Jan 06 '19 at 17:59