Marjer
Marjer

Reputation: 1403

Passing multiple values in java

Im new to java. This is my first java code using parser, i was able to check the node value of particular tag by

if(name != null && name.equals("Length"))

how to pass multiple values in name value like name.equals("Length","Length1","Length3")), i have to check around 2000 values

below is partial code

      if (file.exists()) { 
          Document doc = db.parse(file); 
          Element docEle = doc.getDocumentElement(); 

          NodeList Line = docEle.getElementsByTagName("Lineno");
          System.out.println("size " + Line.getLength());

          if(Line != null && Line.getLength() > 0)
          {                      
            if(doc1.getDocumentElement() == null)
            {
                Element root1 = doc1.createElement("Line");    
                doc1.appendChild(root1);   
            }
            for(int j = 0 ;j < nodeValue.getLength();j++)
            {
               Element el = (org.w3c.dom.Element) nodeValue.item(j);
               String name = el.getAttribute("name");
               String value = el.getAttribute("void");
               String valueFromNode = el.getTextContent();
               if(name != null && name.equals("id"))
               {
                  ITEMID =valueFromNode; 
               }
               if(name != null && name.equals("Length"))
               {
                //  System.out.println("This has datcode " + ele);
                  datecode = "Yes";
                  cwi = valueFromNode;
                //  doc1.getDocumentElement().appendChild(doc1.importNode(imported, true));    
                }
             }
        }
    }

Upvotes: 0

Views: 251

Answers (3)

Antoniossss
Antoniossss

Reputation: 32550

If I would need to compare single String with large collection for it existance in it, i would use HashSet<String>. Insteed of testing equality, I would check if my String object is in my set. If it does - than it is equal.

HashSet<String> compSet=new HashSet<String>();
//put strings to comparision
if(name!=null && compSet.contains(name)){
//hey it was equal.
}

Upvotes: 0

Anubian Noob
Anubian Noob

Reputation: 13596

Put all the values in an ArrayList and use .contains():

List<String> strings = new ArrayList<>();
strings.add("Length");

And then:

if (name != null && strings.contains(name))

Upvotes: 1

Salah
Salah

Reputation: 8657

You can do this by filling your lengths values into ArrayList, then ask if the name contains that array.

For Example:

ArrayList<String> lengths = new ArrayList<String>();
//add all lengths values into array

if(lengths.contains(name){ 
    //do something
}

Upvotes: 1

Related Questions