Vishal
Vishal

Reputation: 148

Reading from properties file

I've a properties file and trying to read some values based on a condition.

Content of properties file:

    TEST0000000000000000000000000000
    TEST00000000000000000000000000
    TEST000000000000000000000000300
    TEST000000000000000000000000000
    TEST000000000000000000000000000
    TEST000000000000000000050000000
    XXX_LOGIN=//xpath
    XXX_PWD=//xpath

To read properties file I'm using the below code.

public ArrayList<String> getMbunValues() throws IOException {
    Properties p = new Properties();
    ArrayList<String> values = new ArrayList<String>();
        try {
            p.load(new FileInputStream("C:/Testing/Properties.properties"));
        } catch (FileNotFoundException ex) {            
        }

    Enumeration e = p.propertyNames(); 

    for (; e.hasMoreElements();) {
        //System.out.println("size"+e.nextElement());
      //System.out.println(e.nextElement());
      values.add(e.nextElement().toString());
     }

    return values;
  }

}

1.Now I'm trying to get these values in two separate functions. One for values beginning with TEST.

2.The other for XXX_LOGIN and so on

Problem:

For getting the values with TEST I'm able to drop the XXX_LOGIN values bases on the code below.

public ArrayList<String> useMbunValues() throws IOException {
            ArrayList<String> mbunvaluesList = file.getMbunValues();

            Collections.sort(mbunvaluesList);
            for (int i = 0; i < mbunvaluesList.size(); i++) {
                if ((mbunvaluesList.get(i).contains("_"))) {
                    mbunvaluesList.remove(mbunvaluesList.get(i));
                }
            }

            for (int i = 0; i < mbunvaluesList.size(); i++) {
                System.out.println(mbunvaluesList.get(i));
            }
            return mbunvaluesList;
        }

The above function returns

TEST0000000000000000000000000000
TEST00000000000000000000000000
TEST000000000000000000000000300
TEST000000000000000000000000000
TEST000000000000000000000000000
TEST000000000000000000050000000

But the XXX_LOGIN = //xpath function returns the TEST values as well. I've used the below code

//Values assigned to array list
public ArrayList<String> useXpathValues() throws IOException {
        ArrayList<String> xpathvaluesList = new ArrayList<String>();
        xpathvaluesList = file.getMbunValues();

//Traversing array list
        for (int i = 0; i < xpathvaluesList.size();) {
            if (!xpathvaluesList.get(i).contains("_")) {
                xpathvaluesList.remove(xpathvaluesList.get(i));
                System.out.println(xpathvaluesList);
                i=i+1;
        }
    }

    for (int i = 0; i < xpathvaluesList.size(); i++) {
        System.out.println(xpathvaluesList.get(i));
    }

    return xpathvaluesList;
}

The output of the above function is as follows

TEST0000000000000000000000000000

TEST000000000000000000000000300

XXX_LOGIN

But I need only the values with the underscore character.

Can someone help me out please?

Thanks, Vishal

Upvotes: 2

Views: 1115

Answers (1)

A4L
A4L

Reputation: 17595

Look at this loop:

for (int i = 0; i < xpathvaluesList.size();) {
    if (!xpathvaluesList.get(i).contains("_")) {
        xpathvaluesList.remove(xpathvaluesList.get(i));
        System.out.println(xpathvaluesList);
        i = i + 1;
    }
}

When does i gets incremented? Only after an element containing an underscore has been removed. Otherwise you'll run in an endless loop or if all elements have underscores you'll end up with an empty list.

Furthermore the condition is i < xpathvaluesList.size(), but the size is changing as you remove elements from the list, and as i grows when you remove elements this means that you will never traverse the whole list and thus end up with elements remaining in the list which you don't need.

To solve the problem with your current approach, use a different list to save the values you need.

If you want to use the same List then use an Iterator and its remove method.

public ArrayList<String> useXpathValues() throws IOException {
    ArrayList<String> xpathvaluesList = getMbunValues();
    Iterator<String> iterator = xpathvaluesList.iterator();

    while(iterator.hasNext()) {
        String next = iterator.next();
        if (! next.contains("_")) {
            iterator.remove();
        }
    }

    for (int i = 0; i < xpathvaluesList.size(); i++) {
        System.out.println(xpathvaluesList.get(i));
    }
    return xpathvaluesList;
}

this prints:

XXX_PWD
XXX_LOGIN

But as M. Deinum suggested, you should better read the file line by line, parse each and build both lists in the same time.

Upvotes: 1

Related Questions