我想用JFreeChart制作一个饼图并使用我从XML文件中获取的变量
Methode类:
package Statistik;
import xmlModul.XmlLoader;
public class Data {
XmlLoader xml = new XmlLoader();
int deleted;
int created;
int edited;
public int getDeleted() {
return deleted;
}
public void setDeleted(int deleted) {
this.deleted = deleted;
}
public int getCreated() {
return created;
}
public void setCreated(int created) {
this.created = created;
}
public int getEdited() {
return edited;
}
public void setEdited(int edited) {
this.edited = edited;
}
public void setDeletedValue(String s) {
xml.setStringByTagName("Deleted", "Statistik", s);
}
public void setCreatedValue(String s) {
xml.setStringByTagName("Created", "Statistik", s);
}
public void setEditedValue(String s) {
xml.setStringByTagName("Edited", "Statistik", s);
}
public int getDeletedValue() {
return Integer.parseInt(xml.getStringByTagName("Deleted", "Statistik"));
}
public int getCreatedValue() {
return Integer.parseInt(xml.getStringByTagName("Created", "Statistik"));
}
public int getEditedValue() {
return Integer.parseInt(xml.getStringByTagName("Edited", "Statistik"));
}
public void setAll(String d, String c, String e){
xml.setStringByTagName("Deleted", "Statistik", d);
xml.setStringByTagName("Created", "Statistik", c);
xml.setStringByTagName("Edited", "Statistik", e);
}
}
统计类中的按钮调用:
DefaultPieDataset pieDataset = new DefaultPieDataset();
Data data = new Data();
int created = data.getCreatedValue();
int deleted = data.getDeletedValue();
int edited = data.getEditedValue();
pieDataset.setValue("one", new Integer(15));
pieDataset.setValue("two", new Integer(40));
pieDataset.setValue("created", new Integer(created));
pieDataset.setValue("edited", edited);
pieDataset.setValue("deleted", deleted);
JFreeChart chart = ChartFactory.createPieChart("Pie Chart", pieDataset, true, true, true);
ChartFrame frame = new ChartFrame("Pie Chart", chart);
frame.setSize(600, 800);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
在我尝试使用变量之前,我只使用了
之类的语句pieDataset.setValue("one", new Integer(15));
它有效。但是现在我尝试使用int
变量deleted
,edited
和created
,但它无论如何都不起作用。
这是我的错误输出:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 0 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at Statistik.Data.getCreatedValue(Data.java:65)
at Statistik.StatistikWithFreeChart_USE_THIS.jButton1ActionPerformed(StatistikWithFreeChart_USE_THIS.java:65)
at Statistik.StatistikWithFreeChart_USE_THIS.access$000(StatistikWithFreeChart_USE_THIS.java:8)
at Statistik.StatistikWithFreeChart_USE_THIS$1.actionPerformed(StatistikWithFreeChart_USE_THIS.java:26)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
修改 我尝试用
设置数据data.setAll("10", "20", "5");
并且它工作但是当我在另一个按钮中使用此方法时,它不会取出这个值,所以我需要在方法中设置它,但我必须从xml中取出它。
答案 0 :(得分:2)
问题是您正在解析XML文件中的数字,但您的数字周围有空格。方法Integer.parseInt
不接受数字周围的空格,传递给它的字符串只能 包含数字。
在Java中,您可以使用方法String.trim()
来删除字符串开头和结尾的空格。您可以使用它来清除从XML文件中获取的数字,然后再将其传递给Integer.parseInt
(或者,您编写代码使用像JAXB这样的XML数据绑定工具,但只有在您的XML格式具有适当的XML格式时才有效XML模式定义)
要修复您的代码(请注意,Integer.parseInt
的所有行最后都有trim()
):
public int getDeletedValue() {
return Integer.parseInt(xml.getStringByTagName("Deleted", "Statistik").trim());
}
public int getCreatedValue() {
return Integer.parseInt(xml.getStringByTagName("Created", "Statistik").trim());
}
public int getEditedValue() {
return Integer.parseInt(xml.getStringByTagName("Edited", "Statistik").trim());
}
答案 1 :(得分:2)
错误消息告诉您需要知道的全部内容:输入字符串中有空格,因此无法将字符串解析为整数。
因此,在您的函数getDeletedValue()
,getCreatedValue()
和getEditedValue()
中,您需要执行trim()以删除这些不必要的空白。例如:
public int getCreatedValue() {
return Integer.parseInt(xml.getStringByTagName("Created", "Statistik").trim());
}