Please, I am working on a project where I have to traverse an RDF(turtle format) and find a certain line from which I could store the preceeding lines from that line. Basically, I am to compare each traversing line with the string :

"[ a sswap:Subject , d:investment;"

.I tried the contains() method and the equals() method but both didn't work. I have written a simple Java code to do this. The only problem is even though it gets to the desired line, it doesn't go into the for loop to execute. This means the boolean is false but why? Could someone explain what I might be doing wrong?

    .
    .
    .
    if(line =="[ a  sswap:Subject , d:investment ;"){
                            do{

                                for (int i = 0; i < line.length(); i++) {
                                    if (line.charAt(i) == ';') {
                                        arr.add(line); //an arraylist
                                    }
                                }
                                line = it.nextLine(); //loop to the next line
                                //traverse through the characters in the line
                                //if the last character is ";"
                                //store that line.
                            }while(line!="]");
   .
   .
   .

Below is the line in the turtle i am trying to check in the if statement of the java code.

sswap:operatesOn [

rdf:type sswap:Graph ; sswap:hasMapping [

                rdf:type sswap:Subject, d:investment; //trying to capture this..
有帮助吗?

解决方案

As the line you are trying to compare to does not exact match

rdf:type sswap:Subject, d:investment; //trying to capture this..

vs

[ a sswap:Subject , d:investment ;

then either use contains http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)

or indexOf http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)

其他提示

You are comparing string that has empty spaces use the trim to eliminate empty spaces before and after the string and then use the equals method of the string you are comparing;

change this:

line =="[ a  sswap:Subject , d:investment ;"

to:

line.trim().equals("[ a  sswap:Subject , d:investment ;")

But is it a good practise to trim the line of string first before comparing them

Please assign the string to another variable and then compare it in IF statement. like,

String str="[ a  sswap:Subject , d:investment ;";
 if(line ==str)
    OR
 if(line.equals(str))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top