2014-12-08 11 views
1

기본적으로 내가해야 할 일은 각 buttonListeners에서 array1과 array2를 얻는 것뿐입니다.이 작업을 수행하는 방법을 알지 못합니다. array1과 array2가 비어있어 TextMatch 버튼이 작동하지 않습니다. 거기에 buttonListener 클래스에서 그들을 설정하는 방법? 내가 필요 모든 그래서 난 내 TextTools.match을 구현할 수있는 SS와 티티 SStwo로하는 array1과 array2는 설정하는 방법() 방법이다액션 리스너 내부에서 클래스의 배열을 설정할 수 있습니까? JAVA

메인 프로그램 :

import javax.swing.JFrame; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 
import java.io.*; 
import java.util.Scanner; 
import java.util.Arrays; 
import javax.swing.JComboBox; 


public class Lab11 extends JPanel 
{ 
    private SuperString [] array1, array2; 
    private StringBuilder string, string2, string3, string4; //loads SuperStrings faster by appending all at once 
    private JRadioButton occurrence, alphabetical; 
    private JPanel text; 
    private JComboBox<Integer> input; 
    private JLabel label, file1,file2, unique, unique2,textmatch; 
    private JButton load, go,go2, TextMatch; 
    private CountLinkedList<SuperString> words, words3; //Change impliments CountList to extends BinaryCountTree 
    private OrderedLinkedList<SuperString> words2, words4;//Change impliments CountList to extends BinaryCountTree 
    private String filename,filename2; 
    private int width = 450; 
    private int height = 550; 
    private TextArea textarea,textarea2; 
    Scanner scan; 

    public Lab11() 
    { 
    TextListener textlistener = new TextListener(); 
    ButtonListener listener = new ButtonListener(); 
    Button2Listener listener2 = new Button2Listener(); 
    Integer [] select = {1,2,3,4}; 
    input = new JComboBox<Integer>(select); 
    text = new JPanel(new GridLayout(1,2)); 
    go = new JButton("Select Text File 1: "); 
    go2 = new JButton("Select Text File 2: "); 
    TextMatch = new JButton("TextMatch"); 
    textmatch = new JLabel(""); 
    label = new JLabel("N: "); 
    unique = new JLabel(""); 
    unique2 = new JLabel(""); 
    file1 = new JLabel(""); 
    file1.setFont(new Font("Helvetica",Font.PLAIN,24)); 
    unique.setFont(new Font("Helvetica",Font.PLAIN,24)); 
    file2 = new JLabel(""); 
    file2.setFont(new Font("Helvetica",Font.PLAIN,24)); 
    unique2.setFont(new Font("Helvetica",Font.PLAIN,24)); 

    occurrence= new JRadioButton("Occurrence"); 
    occurrence.setMnemonic(KeyEvent.VK_O); 
    occurrence.addActionListener(listener); 
    occurrence.addActionListener(listener2); 
    occurrence.setSelected(true); 


    alphabetical = new JRadioButton("Alphabetical"); 
    alphabetical.setMnemonic(KeyEvent.VK_A); 
    alphabetical.addActionListener(listener); 
    alphabetical.addActionListener(listener2); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(occurrence); 
    group.add(alphabetical); 

    go.addActionListener(listener); 
    go2.addActionListener(listener2); 
    input.addActionListener(listener); 
    input.addActionListener(listener2); 
    TextMatch.addActionListener(textlistener); 

    textarea = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); 
    textarea2 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY); 
    textarea.setFont(new Font("Helvetica",Font.PLAIN,24)); 
    textarea2.setFont(new Font("Helvetica",Font.PLAIN,24)); 
    textarea.setPreferredSize(new Dimension(width,height)); 
    textarea2.setPreferredSize(new Dimension(width,height)); 
    setPreferredSize(new Dimension(1000,700)); 
    text.add(textarea); 
    text.add(textarea2); 
    add(occurrence); 
    add(alphabetical); 
    add(label); 
    add(input); 
    add(go); 
    add(file1); 
    add(unique); 
    add(go2); 
    add(file2); 
    add(unique2); 
    add(TextMatch); 
    add(textmatch); 

    add(text); 

    textarea.setText("No File Selected"); 
    textarea2.setText("No File Selected"); 


    } 
    public class ButtonListener implements ActionListener //makes buttons do things 
    { 
     JFileChooser chooser = new JFileChooser("../Text"); 

     public void actionPerformed(ActionEvent event) 
     { 
     Integer N = input.getSelectedIndex()+1; 
     string = new StringBuilder(); 
     string2 = new StringBuilder(); 


     if(event.getSource() == go) 
     { 

     int returnvalue = chooser.showOpenDialog(null); 

    if(returnvalue == JFileChooser.APPROVE_OPTION) 
    { 
     try 
     { 
     File file = chooser.getSelectedFile(); 
     String text1= file.getName(); 
     file1.setText(text1); 
     filename = file.getName(); 
     System.err.println(filename); 
     scan = new Scanner(file); 

     } 
     catch (IOException e) 
     { 
     System.err.println("IO EXCEPTION"); 
     return; 
     }  
    } 
    else 
    { 
     return; 
    } 


    String[] storage = new String[N]; 
    words = new CountLinkedList<SuperString>(); 
    words2 = new OrderedLinkedList<SuperString>(); 
    for(int i=1;i<N;i++) 
    storage[i] = scan.next().toLowerCase().replaceAll("[^A-Za-z0-9]", "") 
        .replaceAll("[.,':]",""); 

    while(scan.hasNext()) 
    { 
     for(int i=0;i<=N-2;i++) 
     storage[i] = storage[i+1]; 
     storage[N-1] = scan.next().toLowerCase(); 
     storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "") 
        .replaceAll("[.,':]",""); 
     SuperString ss = new SuperString(storage); 
     SuperString ss2= new SuperString(storage); 
     words.add(ss); 
     words2.add(ss2); 
    } 
     } 
    SuperString[] array1 = new SuperString[words.size()]; 
    SuperString[] ss = new SuperString[words.size()]; 
    SuperString[] ss2 = new SuperString[words2.size()]; 
    int i=0; 
    int count =0, count2= 0; 

    for(SuperString word: words) 
    { 
     ss[i] = word; 
     array1[i] = word; 
     i++; 
    } 
    int j=0; 
    for(SuperString word: words2) 
    { 
     ss2[j] = word; 
     j++; 
    } 


    Arrays.sort(ss, new SuperStringCountOrder()); 

    for(SuperString word : ss) 
    { 
     count++; 
     string.append(Integer.toString(count)+ "  "+ word+ "\n"); 
    } 
    if(occurrence.isSelected()) 
    { 

     textarea.setText(""); 
    textarea.append(" "+filename+" has wordcount: "+words.size()+ 
     "\n-------------------------\n\n"); 
    textarea.append(string.toString()); 
     string.replace(0,string.length()*2, ""); 
    } 


    for(SuperString word : ss2) 
    { 
     count2++; 
     string2.append(Integer.toString(count2)+ "  "+ word.toString()+ "\n"); 
    } 
     if(alphabetical.isSelected()) 
    { 

     textarea.setText(""); 
     textarea.append(" "+filename+" has wordcount: "+words.size()+ 
     "\n-------------------------\n\n"); 
     textarea.append(string2.toString()); 
     string2.replace(0,string2.length()*2, ""); 

    } 
    unique.setText("Unique Count: "+ Integer.toString(words.size())); 

     } 
    } 


    public class Button2Listener implements ActionListener 
    { 
     JFileChooser chooser = new JFileChooser("../Text"); 


     public void actionPerformed(ActionEvent event) 
     { 
     Integer N = input.getSelectedIndex()+1; 
     string3 = new StringBuilder(); 
     string4 = new StringBuilder(); 

     if(event.getSource() == go2) 
     { 

     int returnvalue = chooser.showOpenDialog(null); 

    if(returnvalue == JFileChooser.APPROVE_OPTION) 
    { 
     try 
     { 
     File file = chooser.getSelectedFile(); 
     String text2= file.getName(); 
     file2.setText(text2); 
     filename2 = file.getName(); 
     System.err.println(filename); 
     scan = new Scanner(file); 

     } 
     catch (IOException e) 
     { 
     System.err.println("IO EXCEPTION"); 
     return; 
     }  
    } 
    else 
    { 
     return; 
    } 

    String[] storage = new String[N]; 
    words3 = new CountLinkedList<SuperString>(); 
    words4 = new OrderedLinkedList<SuperString>(); 
    for(int i=1;i<N;i++) 
    storage[i] = scan.next().toLowerCase().replace(",","").replace(".",""); 

    while(scan.hasNext()) 
    { 
     for(int i=0;i<=N-2;i++) 
     storage[i] = storage[i+1]; 
     storage[N-1] = scan.next().toLowerCase(); 
     storage[N-1] = storage[N-1].replace(",","").replace(".","").replaceAll("[^A-Za-z0-9]", "") 
        .replaceAll("[.,':]",""); 
     SuperString ss = new SuperString(storage); 
     SuperString ss2= new SuperString(storage); 
     words3.add(ss); 
     words4.add(ss2); 
    } 
    textarea2.setText(""); 
     } 
    SuperString[] array2 = new SuperString[words3.size()]; 
    SuperString[] sstwo = new SuperString[words3.size()]; 
    SuperString[] ss2two = new SuperString[words4.size()]; 
    int i=0; 
    int count =0, count2= 0; 

    for(SuperString word2: words3) 
    { 
     sstwo[i] = word2; 
     array2[i] = word2; 
     i++; 
    } 

    int j=0; 
    for(SuperString word2: words4) 
    { 
     ss2two[j] = word2; 
     j++; 
    } 

    Arrays.sort(sstwo, new SuperStringCountOrder()); 

     for(SuperString word2 : sstwo) 
    { 
     count++; 
     string3.append(Integer.toString(count)+ "  "+ word2+ "\n"); 
    } 
    if(occurrence.isSelected()) 
    { 
    textarea2.setText(""); 
    textarea2.append(" "+filename2+" has wordcount: "+words3.size()+ 
     "\n-------------------------\n\n"); 
    textarea2.append(string3.toString()); 
    } 

    for(SuperString word2 : ss2two) 
    { 
     count2++; 
     string4.append(count2+" "+ " "+word2+"\n");  
    } 
    if(alphabetical.isSelected()) 
      { 
     textarea2.setText(""); 
     textarea2.append(" "+filename2+" has wordcount: "+words3.size()+ 
     "\n-------------------------\n\n"); 
     textarea2.append(string4.toString()); 
     } 

    unique2.setText("Unique Count: "+ Integer.toString(words3.size())); 

     } 
    } 

    public class TextListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
     if(event.getSource() ==TextMatch) 
    { 
     textmatch.setText("The match is: "+ TextTools.match(array1,array2)); 
    } 
     } 
    } 



    public static void main(String[] arg) 
    { 
    JFrame frame = new JFrame("Lab 11"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(new Lab11()); 
    frame.setResizable(false); 
    frame.pack(); 
    frame.setVisible(true); 
    } 
} 

텍스트 MATCH 프로그램 :

import java.util.Arrays; 

public class TextTools 
{ 
    public static double match(SuperString[] X, SuperString[] Y) 
    { 
    double length1 = 0, length2 = 0; 

    Arrays.sort(X); 
    Arrays.sort(Y); 
    for(SuperString token : X) 
    { 
     double value = token.count(); 
     length1 += value*value; 
    } 
    length1 = Math.sqrt(length1); 

    for(SuperString token : Y) 
    { 
     double value = token.count(); 
     length2 += value*value; 
    } 
    length2 = Math.sqrt(length2); 
    double total = 0; 
    int j=0; 
    for(int i=0;i<X.length;i++) 
    { 
     while(j < Y.length && X[i].compareTo(Y[j])>0) 
     j++; 
     if(j == Y.length) 
     return total/(length1*length2); 
     if(X[i].compareTo(Y[j]) == 0) 
      total += X[i].count()*Y[j].count(); 
    } 
     return total/(length1*length2); 
    } 
} 
+0

내가 필요한 건 Array1과 array2를 ss와 sstwo로 설정하여 TextTools.ma를 구현할 수있는 방법입니다 tch() 메서드 –

답변

0

밖으로 나 내 array1 및 array2 통해 지우기 해요 ActionListener에 SuperString을 선언하여 다시