2014-03-28 17 views
0

這是我收到的錯誤:在java中我發現了一個-Xlint:未經檢查的錯誤

J:\>javac -Xlint:unchecked Files.java 
Files.java:58: warning: [unchecked] unchecked call to JList(E[]) as a member of 
the raw type JList 
     JScrollPane pane = new JScrollPane(new JList(uniqueWords.toArray())) { 
             ^
    where E is a type-variable: 
    E extends Object declared in class JList 
1 warning 

J:\> 

這是給錯誤代碼:

import java.io.*; 
import java.text.*; 
import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
public class Files 
{ 
    public static void main(String [] args) throws IOException 
    { 
    String filename = ""; 
    String temp; 
    boolean unique = true,found = false; 
    ArrayList<String> passageWords = new ArrayList<String>(); 
    String temporary; 
    String [] words; 
    ArrayList<String> dictionaryWords = new ArrayList<String>(); 
    ArrayList<String> uniqueWords = new ArrayList<String>(); 
    filename = JOptionPane.showInputDialog(null,"Enter the name of the file you would like to display a unique list of words for","Filename input",1); 
    File Passage = new File(filename); 
    Scanner in = new Scanner(Passage); 
    while(in.hasNext()) 
    { 
     temp = in.nextLine(); 
     temp = temp.toLowerCase(); 
     temp = temp.replace("\\s+"," "); 
     temp = temp.replace(".",""); 
     temp = temp.replace("\"",""); 
     temp = temp.replace("!",""); 
     temp = temp.replace("?",""); 
     temp = temp.replace(",",""); 
     temp = temp.replace(";",""); 
     temp = temp.replace(":",""); 
     temp = temp.replace("/",""); 
     temp = temp.replace("\\",""); 
     temp = temp.trim(); 
     words = temp.split(" "); 
     for(int c = 0;c <words.length;c++) 
     passageWords.add(words[c]); 
    } 
    File dictionary = new File("wordList.txt"); 
    Scanner input = new Scanner(dictionary); 
    while(input.hasNext()) 
    { 
     temporary = input.nextLine(); 
     dictionaryWords.add(temporary); 
    } 
    for(int counter = 0;counter<passageWords.size();counter++) 
    { 
    unique = true; 
     for(int count = 0;count<dictionaryWords.size();count++) 
     { 
     if((passageWords.get(counter).contentEquals(dictionaryWords.get(count)))) 
     unique = false; 
     } 
    if(unique) 
    uniqueWords.add(passageWords.get(counter)); 
    } 
    JScrollPane pane = new JScrollPane(new JList(uniqueWords.toArray())) { 
        @Override 
        public Dimension getPreferredSize() { 
         return new Dimension(200, 250); 
        } 
       };; 
     JOptionPane.showMessageDialog(null,pane,"Unique Words",1); 
    for(int counts = 0;counts<uniqueWords.size();counts++) 
    { 
    for(int counters = 1;counters<uniqueWords.size();counters++) 
    { 
    if((uniqueWords.get(counts)).contentEquals(uniqueWords.get(counters))) 
    { 
    uniqueWords.remove(counters); 
    counters--; 
    } 
    } 
    } 
    JOptionPane.showMessageDialog(null,pane,"Unique Words",1); 
    } 
} 

在代碼目前正在閱讀兩個文件,其中一個代表字典和一個文本通道。這是爲了檢查哪些單詞不在字典中並將它們打印出來。代碼中還有一些其他錯誤,但我只是想先排序。

回答

0

JList是一個泛型類型,您正在使用它作爲原始類型。

使用new JList<Object>(uniqueWords.toArray())或者更好,如果你想有一個JList<String>

String[] wordsAsArray = uniqueWords.toArray(new String[uniqueWords.size()]); 
JScrollPane pane = new JScrollPane(new JList<String>(wordsAsArray));  

請注意,你必須比你的代碼更大的問題。第一個是不正確的縮進,這使得它不可讀。第二個是你從主線程中使用Swing組件,雖然它的文檔非常清楚,它們只能從事件派發線程訪問。

+0

我知道縮進問題,我會解決這個問題。我不知道你的意思是關於第二個問題 – Lennygames

+0

謝謝修復了Xlint問題 – Lennygames

相關問題