2014-03-28 21 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

相关问题