我正在进行实习任务,我遇到了异常问题。我使用DOM方法,并在主要方面抛出了SAX异常,但我一直收到错误
CoefficientCalculator.java:13: error: cannot find symbol
public static void main(String[] args)throws ParserConfigurationException, SAXException, IOException
^
symbol: class SAXException
location: class CoefficientCalculator
1 error
当我删除抛出异常时,我会收到其他错误。代码的其余部分发布在下面。有什么想法吗?
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class CoefficientCalculator{
public static void main(String[] args)throws ParserConfigurationException, SAXException, IOException
{
double creditScoreAcc = 0;
double creditSquared;
double paybackPctAcc = 0;
double paybackPctSquared;
double xcrossy;
double coefficient;
int itemCount = 0;
String input1;
String input2;
//Create the Document Builders
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//Build the document
Document document = builder.parse(new File("SampleData.xml"));
//Normalize the XML structure
document.getDocumentElement().normalize();
//Get and dislplay root node
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
//Find mean of credit scores
NodeList list = document.getElementsByTagName("Funding");
for(int i = 0; i < list.getLength(); i++){
Node node =list.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE){
Element element = (Element) node;
input1 = element.getAttribute("creditScore");
double a = Double.parseDouble(input1);
input2 = element.getAttribute("totPymtPct");
double b = Double.parseDouble(input2);
if(a > 0){
creditScoreAcc += a;
paybackPctAcc += b;
itemCount++;
}
}
}
creditSquared = Math.pow(creditScoreAcc, 2.0);
paybackPctSquared = Math.pow(paybackPctAcc, 2.0);
xcrossy = creditScoreAcc * paybackPctAcc;
coefficient = CalcCoefficient(creditScoreAcc, paybackPctAcc, xcrossy,
creditSquared, paybackPctSquared, itemCount);
}
//Method to calculate to Correlation Coefficient of two data sets
public static double CalcCoefficient(double x, double y, double xy, double x2, double y2, int n){
double c = (n)*(xy)-(x * y)/Math.sqrt(n*x2-(Math.pow(x, 2.0)))*Math.sqrt(n*y2-(Math.pow(y, 2.0)));
return c;
}
}
答案 0 :(得分:1)
您不导入org.xml.sax.SAXException
。将其添加到您的代码中然后重试。