所以我有一个关于字典的项目,以显示像普通字典一样的列表。挑战在于,我们应该使用几个实现来显示单词,例如treeDictionary,HashMap等。
当我启动程序时,我必须选择我们想要使用的实现类型,然后选择data.txt(在这个数据中,我们有第一个数组的德语单词和英语单词第二个)
在JTextArea上显示的单词后,我想有这个选项,用户不能关闭程序并再次启动它以选择其他实现和数据等。
但是在我选择了第二个实现之后,eclipse显示了一个错误
线程中的异常" AWT-EventQueue-0" java.lang.ArrayStoreExceptionat java.lang.System.arraycopy(Native Method)
这是我的代码:
{
Component myFrame = null;
static double startTime = 0;
int data[], summeEntry ;
static int size;
static String[] deutsch = new String[16000];
static String[] englisch = new String[16000];
private void ensureCapacity(String o[],int newCapacity){
if(newCapacity < size) {
return;
}
String [] a = o;
data = new int[newCapacity];
System.arraycopy(a, 0, data, 0, size);
}
//open file
public void read(File f) {
LineNumberReader in = null;
try {
in = new LineNumberReader(new FileReader(f));
String line;
int i = 0;
while ((line = in.readLine()) != null) {
String[] sf = line.split(" ");
if (sf.length == 2) {
if(size >= deutsch.length){
ensureCapacity(deutsch, size*2);
ensureCapacity(englisch, size*2);
}
//kamusGUI
kamusGUI.dt.insert(sf[0], sf[1]);
deutsch[i] = sf[0];
englisch[i] = sf[1];
size++;
i++;
} else if (sf.length == 3) { //I just copy from the first if. Then it can calculate the actually number of words
if(size >= deutsch.length){
ensureCapacity(deutsch, size*2);
ensureCapacity(englisch, size*2);
}
kamusGUI.dt.insert(sf[0], sf[1] + sf[2]);
deutsch[i] = sf[0];
englisch[i] = sf[1] + sf[2];
size++;
i++;
} else if(sf.length >= 3){
//wenn ein deutsches Wort mehr als 2 Bedeutungen hat
JOptionPane.showMessageDialog(myFrame, "Hallo, english more than 2 words");
}
}
//to show your entries in output
kamusSearchDelete.tOut.setText(kamusGUI.dt.toString());
//to show how many entries you have from your dictionary
kamusSearchDelete.stsEntry.setText("Total entr(y/ies): " + size + " words");
in.close();
} catch (IOException ex) {
System.out.println("open canceled");
}
}
public static void save(File f) {
PrintWriter out = null;
try {
out = new PrintWriter(f);
out.println(kamusGUI.dt.toString());
out.close();
} catch (IOException ex) {
System.out.println("save canceled");
}
}
我已经构建了另一个JButton来调用main方法(我可以说,我尝试通过这个按钮重置程序),我们可以选择另一个implmentation。但它无法正常工作。也许你们有任何想法,我应该怎么做阵列?我非常感谢每一条建议或想法。
我添加了错误,也许你们想看到它
Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at aufgabe1.kamus.ensureCapacity(kamus.java:35)
at aufgabe1.kamus.read(kamus.java:50)
at aufgabe1.kamusMenu.actionPerformed(kamusMenu.java:156)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
答案 0 :(得分:0)
您正在呼叫System.arraycopy(a, 0, data, 0, size);
。
a
是string array
,而data
是int array
。我们不能将字符串数组复制到int数组,因此也是例外。