L29101987
L29101987

Reputation: 25

Java code to search for strings and other relating strings

In a text file I have the following Entries

1|002|3|Lubna|M|141021081331|

2|003|3|Rashid|1|141021081431|

3|002|3|Amal|M|141021081340|

4|002|3|Lubna|F|141021081531|

I want to create a Java code to check for the existence of |M|; i.e. means Lubna is searching for something. And to check after that if Lubna found what she was searching about, and that will happen if for the same username is followed by |F|.

for example :

1|002|3|Lubna|M|141021081331| ---> Lubna is searching
1|002|3|Lubna|F|141021081531| ---> Lubna found what she was searching about

1|002|3|Amal|M|141021081340|----> Amal is searching

But because there is no amal record include |F| so that mean she is still searching..
Provide the records might be unlimited , i.e. not only 4 as in the above example..
So what I did is the following:

String st;        
// where 14 is num of columns in record , like in our examle they are 6 
String [] subst= new String [14] ; 
while ((st = inputStream.readLine()) != null) {
    if (st.contains("|M|")) {
        for (int i = 0; i < 14; i++) {
            subst=st.split("[|]");  
        }
        System.out.println(subst);
        n++;
    }
    else
    if (st.contains("|F|")) {
        // System.out.println(EmailBody[j]);                    
    }
}

In this if I will manage to make this subst not only 1 dimension array of string, but make it 2, I can search for the existence of |F|, but the problem is that I can't initialize such this kind of arrays because I don't know what its second dimension will be..

Any one can help me if finding an easiest way to achieve my need ??

Upvotes: 0

Views: 83

Answers (3)

benscabbia
benscabbia

Reputation: 18072

"the problem is that I can't initialize such this kind of arrays because I don't know what its second dimension will be.."

You want to look into the collections framework which allows you to create lists without specifying size.

So:

ArrayList<String> list = new ArrayList<String>(); 

Then you can add stufff to list:

list.add("stuff");
list.add("stuff2");
list.add("stuff3");
//as many time as you want

Loop

for(String str : list)
{
    System.out.println(str);
}

And then rest of code.

For a tutorial on collections, checkout caveOfProgramming

Upvotes: 1

Michael D.
Michael D.

Reputation: 1837

load like this, then iterate LinesList

    String filename = "myfile.txt";
    List<String> LinesList = new ArrayList<String>();
    Path filePath = new File(filename).toPath();
    Charset charset = Charset.defaultCharset();
    try {
        LinesList = Files.readAllLines(filePath, charset);
    } catch (Exception e) {
        if (e instanceof NoSuchFileException) {
            System.out.println("error: file not found!");
        } else {
            e.printStackTrace();
            System.exit(101);
        }
    }

Upvotes: 0

drkunibar
drkunibar

Reputation: 1337

Try regular expressions. But be careful with huge amount of data. Could be slow.

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

public class SearchDemo {

    public static void main(String[] args) {
        String data = "1|002|3|Lubna|M|141021081331|\n"
                + "2|003|3|Rashid|1|141021081431|\n"
                + "3|002|3|Amal|M|141021081340|\n"
                + "4|002|3|Lubna|F|141021081531|";

        Pattern p = Pattern.compile("^\\d+[|]\\d+[|]\\d+[|]([^|]+)[|]([M]?)[|]\\d+[|]", Pattern.MULTILINE);
        Matcher m = p.matcher(data);
        while (m.find()) {
            System.out.println("search name: " + m.group(1) + " type: " + m.group(2));
            Pattern p2 = Pattern.compile("^\\d+[|]\\d+[|]\\d+[|](" + m.group(1) + ")[|]([F]?)[|]\\d+[|]", Pattern.MULTILINE);
            Matcher m2 = p2.matcher(data);
            if (m2.find()) {
                System.out.println("found name: " + m2.group(1) + " type: " + m2.group(2));
            } else {
                System.out.println("no match name: " + m.group(1) + " type: " + m.group(2));
            }
        }
    }
}

Upvotes: 0

Related Questions