Domanda

This is the error I am getting:

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:\>

This is the code that is giving the error:

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);
  }
}

The code at the moment is reading two files one of which represents a dictionary and one which is a passage of text. It is meant to check what words aren't in the dictionary and print them out. There are some other errors in the code also but I just want to sort this first.

È stato utile?

Soluzione

JList is a generic type, and you're using it as a raw type.

Use new JList<Object>(uniqueWords.toArray()) or, better, if you want a JList<String>:

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

Note that you have bigger problems than that in your code. The first one is the incorrect indentation, which makes it unreadable. The second one is that you're using Swing components from the main thread, although its documentation is very clear that they can only be accessed from the event dispatch thread.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top