Jason John
Jason John

Reputation: 936

BufferedReader not iterating properly

I'm running into a weird bug I've never seen before in Java. I have a BufferedReader which I'm using to read from a .txt file. I'm trying to grab some text from a few of the lines in my text file

@TextType()
@FindBy(xpath = "//label[normalize-space(text())=\"Premium Vehicle Monthly Rate 1\"]/../following-sibling::td[1]//input")
public WebElement PremiumVehicleMonthlyRate1;

I need to grab Premium Vehicle Monthly Rate 1

Here's relevant code:

public static void main(String[] args) {
    //string to hold the product name
    String productName = "";
    //File object to hold the java source .txt file
    File file = new File(args[0]);
    int count = 0;

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            if(line.contains("TextType")) {
                line = br.readLine();
                String subline = line.substring(line.indexOf("\"")+1);
                subline = subline.substring(subline.indexOf("\"")+1);
                subline = subline.substring(0, subline.indexOf("\"")-1); //COMMENT THIS LINE
                System.out.println(subline);
                count++;
            }
        }
        br.close();
        writer.close();
    }catch (Exception e) {

    }
    System.out.println("Count: " + count);
}

Doing this, the code works, but only for the first 21 elements in my list.

If I comment the noted line in the code, then it works for all (105 total "TextType") elements in my text file, but obviously not the substring I'm looking for.

What is going on!?

Upvotes: 1

Views: 52

Answers (1)

dsh
dsh

Reputation: 12234

Your data is probably not what you expect it to be. I suspect an exception is thrown, which you catch and then ignore. Add e.printStackTrace() in your catch block and then you'll see the error.

Upvotes: 2

Related Questions