📄 search.java
字号:
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class search extends HandlerBase
{
private double totalPrice = 0.0;
private double averagePrice = 0.0;
private int count = 0;
static private Writer out;
public static void main(String argv[]) throws Exception
{
/*if(argv.length!=1)
{
System.err.println("Usage: cmd filename");
System.exit(1);
}*/
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
out = new OutputStreamWriter(System.out,"UTF-8");
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File("money.xml"),new search());
}
catch(Throwable t)
{
t.printStackTrace();
}
}
public void startElement(String name,AttributeList attrs) throws SAXException
{
if(name.equals("month"))
{
count = count + 1;
double price = new Double(attrs.getValue("price")).doubleValue();
totalPrice += price;
}
}
public void endDocument()
{
averagePrice = totalPrice/count;
System.out.println("the average money every month is "+averagePrice);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -