-2

I have a long stiring as

String myStr = "PRODUCTION PERIOD 15 DAYS VALIDITY 30 DAYS TOTAL 2.117 BOXES VOLUME 231,78 m 3 NET WEİGHT 10.588,50 kg GROSS WEİGHT 11.700,00 kg"

I read this String from a pdf file. Format never changes. But the values are changing.

I want to get the Validity = 30, Total : 2117 Net Wight : 10.588,50 etc. Bold words changing at every pdf file.

I dont know how to get these values. Any help appiciated.

Clashsoft
  • 11,553
  • 5
  • 40
  • 79
Turgut Tak
  • 43
  • 8
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Jan 02 '18 at 18:55
  • *"Format never changes. But the values are changing. Bold words changing at every pdf file."* - which one is it? What is fixed, what is not, if *you* can see a pattern the computer can as well, simply implement the way *you* can extract the values. Maybe "regex" is of use, don't know yet. – luk2302 Jan 02 '18 at 18:55
  • "PRODUCTION PERIOD **15** DAYS VALIDITY **30** DAYS TOTAL **2.117** BOXES VOLUME **231,78** m 3 NET WEİGHT **10.588,50** kg GROSS WEİGHT **11.700,00 kg**" bold words change – Turgut Tak Jan 02 '18 at 18:58
  • One solution can be to use string.split() [https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29] – ajc Jan 02 '18 at 19:00
  • You can use regular expression capturing groups. You will need to research that yourself. – Steve11235 Jan 02 '18 at 19:03

2 Answers2

0

You can use myStr.split() method, and then iterate over the words. For Example:

    String myStr = "PRODUCTION PERIOD 15 DAYS VALIDITY 30 DAYS TOTAL 2.117 BOXES VOLUME 231,78 m 3 NET WEİGHT 10.588,50 kg GROSS WEİGHT 11.700,00 kg";
    String[] words = myStr.split(" ");
    for (int i = 0; i < words.length; i++){
        if (words[i].toLowerCase().equals("validity")){
            System.out.println("Validity is " + words[i + 1]);
        }

        if (words[i].toLowerCase().equals("total")){
            System.out.println("Total is " + words[i + 1]);
        }

        if (words[i].toLowerCase().equals("volume")){
            System.out.println("Volume is " + words[i + 1]);
        }
    }

The Output :

Validity is 30
Total is 2.117
Volume is 231,78
Ron Badur
  • 1,873
  • 2
  • 15
  • 34
  • **Do not feed the help vampires!** Just look at his questions. Every single one a low quality "send me teh codez" help vampire question! –  Jan 02 '18 at 20:21
  • It's depends on point of view, I dont see him as a vampire but as a beginner developer who wants to learn new ways to solve his problems – Ron Badur Jan 02 '18 at 20:47
-1

You can use following code. Try running it and see if this is what you are looking for. You can iterate over pdf files and execute this is loop if required to process 1 pdf at a time:

    String myStr = "PRODUCTION PERIOD 15 DAYS VALIDITY 30 DAYS TOTAL 2.117 BOXES VOLUME 231,78 m 3 NET WEIGHT 10.588,50 kg GROSS WEİGHT 11.700,00 kg";
    int idxValidity = myStr.indexOf("VALIDITY");
    int idxDays = myStr.indexOf("DAYS TOTAL");
    int idxTotal = myStr.indexOf("TOTAL");
    int idxBoxes = myStr.indexOf("BOXES");
    int idxWeight = myStr.indexOf("WEIGHT");
    int idxKg = myStr.indexOf("kg");

    System.out.println((myStr.substring(idxValidity, idxDays)).trim().replace(" ", "="));
    System.out.println(myStr.substring(idxTotal, idxBoxes).trim().replace(" ", "="));
    System.out.println(myStr.substring(idxWeight, idxKg).trim().replace(" ", "="));
user3731930
  • 137
  • 1
  • 1
  • 8
  • **Do not feed the help vampires!** Just look at his questions. Every single one a low quality "send me teh codez" help vampire question! –  Jan 02 '18 at 20:21