我的项目应该对多项式进行基本操作。每个多项式都是来自类Monom
的对象的ArrayList。
我用Swing创建了一个界面。我创建了一个方法,将收到的String转换为Polinom
对象。如果我使用基本命令,比如当我按下按钮,获取文本并显示它时,一切正常。但是当我调用前面提到的方法(toPolinom
)时,按钮不再起作用,并且在控制台中写入了红线。
这是按钮代码的样子:
mybutton = new JButton("Suma");
mybutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pol1 = numberField1.getText();
String pol2 = numberField2.getText();
polinom1 = new Polinom(p1);
polinom2 = new Polinom(p2);
polinom1 = polinom1.toPolinom(pol1);
polinom2 = polinom2.toPolinom(pol2);
resultField.setText(polinom1.suma(polinom1, polinom2)
.toString());
}
});
mybutton2 = new JButton("Diferenta");
mybutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pol1 = numberField1.getText();
String pol2 = numberField2.getText();
polinom1 = polinom1.toPolinom(pol1);
polinom2 = polinom2.toPolinom(pol2);
resultField.setText(rezultat.diferenta(polinom1, polinom2)
.toString());
}
});
resultField
和numberField1
是JTextField
类型;
这是toPolinom()
课程中的Polinom.java
方法:
public Polinom toPolinom(String p1) {
List<Monom> prez = new ArrayList<Monom>();
int grad, coef;
int i = 0;
for(i=0; i<p1.length()-1; i=i+5){
grad=0; coef=0;
if(p1.charAt(i)=='+') coef=Character.getNumericValue(p1.charAt(i+1));
else if (p1.charAt(i)=='-') coef=-Character.getNumericValue(p1.charAt(i+1));
grad=Character.getNumericValue(p1.charAt(i+4));
prez.add(new MonomZ(grad, coef));
}
return new Polinom(prez);
}
我测试了这种方法,它有效。
最后,这是我点击按钮时收到的内容:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at tema1.Polinom.toString(Polinom.java:208)
at tema1.Fatza$1.actionPerformed(Fatza.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
警告方法补充道:
public String toString() {
String Rezultat = "";
for (int i = 0; i < polinom.size() - 1; i++) {
Rezultat = Rezultat + verificare(polinom.get(i), polinom, i);
}
return Rezultat
+ verificare(polinom.get(polinom.size() - 1), polinom, //line 208
polinom.size() - 1);
}
private String verificare(Monom monom, List<Monom> polinom, int n) {
// functia verifica daca trebuie adaugat semn sau nu
String text;
boolean ok = true;
for (int i = 0; i < n; i++) {
if (polinom.get(i).getCoef().doubleValue() != 0) {
ok = false;
break;
}
}
double numar = monom.getCoef().doubleValue();
if (ok == true && numar > 0) {
text = monom.toString();
} else {
text = "+" + monom.toString();
}
if (numar < 0) {// numarul are inclus semn negativ
text = monom.toString();
}
return text;
}
如果我忘记了任何信息,请问我。
答案 0 :(得分:3)
你正在打电话
verificare(polinom.get(polinom.size() - 1), polinom,' 'polinom.size() - 1);
并获得带有-1值的ArrayIndexOutOfBoundsException
,表明当您收到错误时,您的polinom集合的大小为0
。检查这一点,如果大小为0,则不要调用此方法。
答案 1 :(得分:3)
在你的方法中:
public Polinom toPolinom(String p1) {
....
for(i=0; i<p1.length()-1; i=i+5){
你将每个循环增加约5
所以想象一下这个字符串&#34; 1234567&#34;: 第一个循环你得到12345,下一循环只有67,但你试着这样做:
grad=Character.getNumericValue(p1.charAt(i+4));
此刻,i + 4 = 9,这是outOfBound ...... 我猜(没有测试过,你没有显示出错的行)这是你的问题
答案 2 :(得分:0)
在我修改方法toString()
和verificare()
之后,一切都运行良好,如下所示:
public String toString() {
int ok = 0;
String Rezultat = "";
for (int i = 0; i < polinom.size(); i++) {
Rezultat = Rezultat + verificare(polinom.get(i), polinom, i);
if (polinom.get(i).getCoef().doubleValue() != 0)
ok = 1;
}
if (ok == 1)
return Rezultat;
else
return "0";
}
private String verificare(Monom monom, List<Monom> polinom, int n) {
String text = "";
double numar = monom.getCoef().doubleValue();
if (n != 0 && numar > 0) {
text = "+" + monom.toString();
} else
text = monom.toString();
if (numar < 0) {// numarul are inclus semn negativ
text = monom.toString();
}
return text;
}
非常感谢你的帮助!