如何在if条件下计算字符串中的字符数

时间:2018-02-14 09:29:30

标签: java

我们一直试图限制名字中的字符数,比如名字不应该超过4个。

而且,我们需要确保薪水应该是100000。

有人可以帮助我们解决这个问题吗?

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
public class ReadXml_File {

    public static void main(String[] args)
    {
        try
        {
        File hxmlfile = new File("C:\\myjava\\staff.xml");
        DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuild = dbFact.newDocumentBuilder();
        Document doc = dBuild.parse(hxmlfile);
        doc.getDocumentElement().normalize();
        System.out.println("Root Element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("staff");
        System.out.println("----------------------------------------");
        for(int temp = 0;temp<nList.getLength(); temp++)
        {
            Node nNode = nList.item(temp);
            System.out.println("\n Current Element :" + nNode.getNodeName());
            if(nNode.getNodeType() == Node.ELEMENT_NODE)
            {
                Element eElement = (Element) nNode;
                System.out.println("Staff id :" + eElement.getAttribute("id"));             
                System.out.println("First Name :" + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println("Last Name :" + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println("Nick Name :" + eElement.getElementsByTagName("nickname").item(0).getTextContent());
                System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());                     
            }   
        }
        }
        if (count.id    )
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
<?xml version="1.0"?>
<company>
    <staff id="1001">
        <firstname>Harikrishnan</firstname>
        <lastname>S R</lastname>
        <nickname>Hari</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001">
        <firstname>Ashwin</firstname>
        <lastname>Suma</lastname>
        <nickname>Ashwin</nickname>
        <salary>200000</salary>
    </staff>
    <staff id="3001">
        <firstname>Ebin</firstname>
        <lastname>Mathew</lastname>
        <nickname>Ebin</nickname>
        <salary>300000</salary>
    </staff>
</company>

1 个答案:

答案 0 :(得分:0)

要打印出有效行,请修改您的循环内容,如下所示:

Element eElement = (Element) nNode;
String firstName = eElement.getElementsByTagName("firstname").item(0).getTextContent();
// You might want to catch NumberFormatException here if input is not a valid integer
int salary = Integer.parseInt(eElement.getElementsByTagName("salary").item(0).getTextContent());

if (firstName.length() > 4) {
  System.err.println("Validation failed-count is more than 4");
} else if (salary != 100000) {
  System.err.println("Validation failed-salary is not 100000");
} else {
  System.out.println("Staff id :" + eElement.getAttribute("id"));             
  System.out.println("First Name :" + firstName);
  System.out.println("Last Name :" + eElement.getElementsByTagName("lastname").item(0).getTextContent());
  System.out.println("Nick Name :" + eElement.getElementsByTagName("nickname").item(0).getTextContent());
  System.out.println("Salary : " + salary);             
}

这将检查名字的长度是否超过4个字符或者工资是否等于100000并打印相应的错误。