gpt4 book ai didi

java - 使用抛出 SAXException 的方法编译类时出现问题

转载 作者:行者123 更新时间:2023-12-01 11:59:07 25 4
gpt4 key购买 nike

我正在执行实习任务,但遇到了异常问题。我使用 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;
}
}

最佳答案

您没有导入org.xml.sax.SAXException。将其添加到您的代码中并重试。

关于java - 使用抛出 SAXException 的方法编译类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28119884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com