所以我有一個項目是關於一個字典顯示列表像一個正常的字典。目前的挑戰是,我們應該用一對夫婦實現顯示的話,像treeDictionary,HashMap和等不同實現的字典; java.lang.ArrayStoreException
當我啓動的程序,我要選擇我們要使用什麼樣的實現,然後我選擇data.txt(在這個數據中,我們有第一個數組的德語單詞和第二個單詞的英文單詞)
在JTextArea上顯示單詞後,我想要有這個選項,不得關閉程序並重新啓動以選擇另一個實現和數據等。
但經過我選用第2執行,日食顯示我的錯誤
異常在線程 「AWT-EventQueue的 - 0」 java.lang.ArrayStoreExceptionat java.lang.System.arraycopy(本機方法)
- 它着重於我的ensureCapasity方法。
這是我的代碼:
{
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調用的主要方法(我可以說,我嘗試重置通過該按鈕的程序),我們可以選擇另一種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)
請將具有完整回溯的異常添加到問題本身中。 –
@Antti ive新增了 –