有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java问题使用引发SAXException的方法编译类

我正在做一个实习任务,我遇到了一个例外。我正在使用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;
   }
}

共 (1) 个答案

  1. # 1 楼答案

    不导入org.xml.sax.SAXException。将其添加到代码中,然后重试