Java matcher and pattern code.

I am working with matcher and pattern in Java. I think this test code should work to add and print my desired search but it is not. If anyone can take a look and fix this it would be much appreciated. Note the if I just use the while loop it works fine. It is when I add in the for loop to iterate through the array list is when it prints out blank.

Screen-Shot-2018-08-29-at-11.51.28-A.png
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();

        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");

        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            String so1 = "SO#";
            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {


                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(i) == ' ') {
                        foundspace = true;
                    }
                    j++;

                }

                String d = line.substring(m.start(), j);
                linet.add(d);

            }

            System.out.println(linet);
            }
        }

    }
E RData AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
Could you please post the code as text?
0
E RData AnalystAuthor Commented:
public class test {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();

        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");

        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            String so1 = "SO#";
            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {


                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(i) == ' ') {
                        foundspace = true;
                    }
                    j++;

                }

                String d = line.substring(m.start(), j);
                linet.add(d);

            }

            System.out.println(linet);
            }
        }

    }
0
d-glitchCommented:
It is MUCH better to paste your code into the question than to include a screenshot.

Maybe I have a simple fix I would like to try, I if I have to retype the whole thing I will be moving on.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

E RData AnalystAuthor Commented:
I paste it in comments. Sorry.
0
E RData AnalystAuthor Commented:
I have pasted in orignal question.
1
CEHJCommented:
It is MUCH better to paste your code into the question than to include a screenshot.
And better still to post it in code tags

What are you trying to do exactly?
0
E RData AnalystAuthor Commented:
My overall goal is to extract SO#833452-14943408 from the string in the array list from my test string. This is part of a larger program in which I am reading in lines from a file line by line and trying to extract any instance of "SO#" and the subsequent following number and add it to an array list.
0
E RData AnalystAuthor Commented:
I have modified the code a bit and I think I am heading in the right direction but now it prints out iterations of the array. Code below.



import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test2 {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();
        String so1 = "SO#";

        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");


        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {

                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(j) == ' ') {
                        foundspace = true;
                    }
                    j++;
                }

                String d = line.substring(m.start(), j);
                linet.add(d);

            }


        }

                for (int k = 0; k < linet.size(); k++) {

                    System.out.println(linet.get(k));
                }
            }
        }
0
E RData AnalystAuthor Commented:
So I guess my question becomes a bit easier. Given the updated comment code. I get my end result but I get every iteration of my result and I just want the last one. My problem is how to pass a variable from inside a loop to outside of it or is there another way to write this code. I am new to java.
0
CEHJCommented:
I get my end result but I get every iteration of my result and I just want the last one.
Don't quite follow that. I thought you wanted all SOs? Do you mean you want all unique instances of that or just one? Confused

Also, you're reading finally lines from a file are you?
0
E RData AnalystAuthor Commented:
I actually just figured this out. Below is the code that prints the desired  output. Thanks

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test2 {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();
        String so1 = "SO#";

        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("S)#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - SO#Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");


        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {

                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(j) == ' ') {
                        foundspace = true;
                    }

                    j++;
                }

                String d = line.substring(m.start(), j);
                linet.add(d);
            }



        }

                for (int k = 0; k < linet.size(); k++) {

                    System.out.println(linet.get(k));
                }
            }
        }
0
d-glitchCommented:
I see this is your first question on ExEx.  If you are going to be posting more code samples, you might want to use the CODE tags to display your program snippets.  You can cut and paste your code into the question, then select it, and click the CODE icon on the menu bar.  Ckick on Preview to see the result.
        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();
        String so1 = "SO#";

        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("S)#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");

Open in new window

0
CEHJCommented:
You don't need to do all that space searching btw. Put it in the pattern
0
Jan LouwerensSoftware EngineerCommented:
      List<String> lines = Arrays.asList(
         "SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]",
         "S)#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]",
         "O#833452-14943408 PO#Subject: Re: gg  - SO#Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]",
         "CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");
      
      List<String> results = new ArrayList<>();
      
      Pattern pattern = Pattern.compile("SO#\\S+");
      
      for (String line : lines)
      {
         Matcher matcher = pattern.matcher(line);
         while (matcher.find())
            results.add(matcher.group());
      }
      
      for (String result : results)
         System.out.println(result);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
E RData AnalystAuthor Commented:
Thanks all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.