-1

I extracted data from the xpath using HtmlUnit and have this:

String[] data = new String[10]; // number of columns

data[0] = page.querySelector(".sprop-product-heading").getTextContent().trim().toString();

data[d]... so on.

But when I use the data[0] as a parameter in lastIndexOf, it returnes -1.

int posProd = someList.toString().lastIndexOf(data[0]); //returns -1

But when I use "Searched Words in quotations" as parameter of lastIndexOf, it returns the exact position.

int posProd = someList.toString().lastIndexOf("Searched Words in quotations"); //returns the index position

I have already tried save the data[0] on another variable but still it did not work, it returns -1.

String prodname = String.valueOf(data[0]);
int posProd = someList.toString().lastIndexOf(prodname); //returns -1

How could I use the data[0] as the parameter in lastIndexOf without using the ""?

EDIT:

Example:

data[0] = "Lorem Ipsum"

String someList = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. "

int posProd = someList.toString().lastIndexOf(data[0]); //returns -1

But When I test it to:

int posProd = someList.toString().lastIndexOf("Lorem Ipsum"); //returns the position

EDIT 2:

    List<HtmlDivision> productDesc = page.getByXPath("//div[@class='col-md-6 md-margin-bottom-10']//following-sibling::div");
    String productDescList = "";
    for(HtmlDivision prodName:productDesc){
        productDescList = productDescList.trim()+prodName.asText().trim();
    }
    System.out.println("productDescList: " +productDescList);
    String prodname = String.valueOf(data[0]);
    System.out.println("prodname: " +prodname.trim());
    int posProd = productDescList.toString().lastIndexOf(prodname);
    String cleanDesc = productDescList.substring(posProd, productDescList.length()-5);
    System.out.println("cleanDesc: " +cleanDesc);

https://prnt.sc/fwxc24

jameslem
  • 85
  • 2
  • 12
  • 1
    If `someList.toString().lastIndexOf(data[0]) != someList.toString().lastIndexOf("Searched Words in quotations")`, then obviously `data[0] != "Searched Words in quotations"`. Debugging your code would quickly demonstrate the same. – shmosel Jul 17 '17 at 21:39
  • @shmosel sorry I forgot to exphasize, "Searched Words in quotations" is just a test to see if it will return a position. I can only use data[0] as parameter that came from xpath. – jameslem Jul 17 '17 at 21:42
  • 1
    Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your problem. – Joe C Jul 17 '17 at 21:44
  • It's not clear what your question is. Are you asking why `lastIndexOf()` returns -1? Obviously because the value you're searching for is not found. – shmosel Jul 17 '17 at 21:44
  • @shmosel I made an edit to the question for question clarification. – jameslem Jul 17 '17 at 21:48
  • Your assertion is easily disproven: http://ideone.com/lsA330. Please post a [mcve] so we have an identifiable issue to discuss. – shmosel Jul 17 '17 at 21:59
  • @shmosel I got you point, but eclipse keeps on existing. https://prnt.sc/fwxc24 – jameslem Jul 17 '17 at 22:27
  • That's no help. We can't diagnose a problem from a screenshot. – shmosel Jul 17 '17 at 22:28
  • @shmosel I added the code that generates the screenshot.(at EDIT 2). Thanks for responding. – jameslem Jul 17 '17 at 22:37
  • You're not including the code that populates `data[0]`. – shmosel Jul 17 '17 at 22:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149424/discussion-between-shmosel-and-jameslem). – shmosel Jul 17 '17 at 22:41
  • 1
    Per our chat, this is a dupe of https://stackoverflow.com/questions/28295504/how-to-trim-no-break-space-in-java – shmosel Jul 17 '17 at 23:52

1 Answers1

0

I found out where the error is. It's on the content of data[0].

data[0] = page.querySelector(".sprop-product-heading").getTextContent().trim().toString();

The output data[0] does contains something else other than the string.

As per the help of shmosel:

System.out.println(Arrays.toString(data[0].toCharArray()));
System.out.println(Arrays.toString(data[0].getBytes()));

We recognize that it contains "non-break space" added at the end of the string array.

This solves it: How to trim no-break space in Java?

string.replaceAll("(^\\h*)|(\\h*$)","")
jameslem
  • 85
  • 2
  • 12