繁体   English   中英

无法将其中一行附加到JTextArea

[英]Can't append one of the lines to JTextArea

import java.awt.BorderLayout;

import java.io.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.io.FilenameFilter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.commons.io.FileUtils;

@SuppressWarnings("unused")
public class readFile extends JFrame {

JTextArea _resultArea = new JTextArea(100, 100);



JScrollPane scrollingArea = new JScrollPane(_resultArea);

final String newline = "\n";


public readFile(){

    File folder = new File("C:\\Users\\user\\fypworkspace\\FYP");
    File[] listOfFiles = folder.listFiles();
    int numDoc = 0;

    for (int i = 0; i < listOfFiles.length; i++) {
        if ((listOfFiles[i].getName().endsWith(".txt"))) {
            numDoc++;
        }
    }

    System.out.println("The number of files is this folder is : " + numDoc);

    // Calculating term frequency
    int filename = 11;
    String[] fileName = new String[filename];
    int a = 0;
    int totalCount = 0;
    int wordCount = 0;

    // Count the number of documents containing the query

    System.out.println("Please enter the query  :");
    Scanner scan2 = new Scanner(System.in);
    String word2 = scan2.nextLine();
    String[] array2 = word2.split(" ");
    int[] numofDoc = new int[array2.length];

    for (int b = 0; b < array2.length; b++) {

        numofDoc[b] = 0;

        for (int i = 0; i < filename; i++) {

            try {

                BufferedReader bf = new BufferedReader(new FileReader(
                        "C:\\Users\\user\\fypworkspace\\FYP\\abc"
                                + i + ".txt"));

                int matchedWord = 0;

                Scanner s2 = new Scanner(bf);

                {

                    while (s2.hasNext()) {
                        if (s2.next().equals(array2[b]))
                            matchedWord++;
                    }

                }
                if (matchedWord > 0)
                    numofDoc[b]++;

            } catch (IOException e) {
                System.out.println("File not found.");
            }

        }
        _resultArea.append(array2[b]
                + " --> This number of files that contain this term  "
                + numofDoc[b]+ newline);
    }

    // calculate TF-IDF (TermFrequency/InverseTermFrequency)

    int queryVector = 1;
    double similarity = 0.0;
    int wordPower;



    double[] similarityScore = new double [11];




    double[][] arrays = new double[11][1];

    for (a = 0; a < filename; a++) {
        int totalwordPower = 0;
        int totalWords = 0;
        try {
            _resultArea.append(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  "+ newline);
            _resultArea.append("\n"+ newline);
            _resultArea.append("The word inputted : " + word2+ newline);
            File file = new File(
                    "C:\\Users\\user\\fypworkspace\\FYP\\abc"
                    + a + ".txt");
            _resultArea.append(" _________________"+ newline);

            _resultArea.append("| File = abc" + a + ".txt | \t\t \n"+ newline);

            for (int i = 0; i < array2.length; i++) {

                totalCount = 0;
                wordCount = 0;

                Scanner s = new Scanner(file);
                {
                    while (s.hasNext()) {
                        totalCount++;
                        if (s.next().equals(array2[i]))
                            wordCount++;

                    }

                    _resultArea.append(array2[i] + " --> Word count =  "
                            + "\t " + "|" + wordCount + "|"+ newline);
                    _resultArea.append("  Total count = " + "\t " + "|"
                            + totalCount + "|"+ newline);
                    System.out.printf("  Term Frequency =  | %8.4f |",
                            (double) wordCount / totalCount );

                    _resultArea.append("\t "+ newline);

                    double inverseTF = Math.log10((float) numDoc
                            / (numofDoc[i]));
                    _resultArea.append("    --> IDF = " + inverseTF+ newline);

                    double TFIDF = (((double) wordCount / totalCount) * inverseTF);
                    _resultArea.append("    --> TF/IDF = " + TFIDF + "\n"+ newline);

                    totalWords += wordCount;

                    wordPower = (int) Math.pow(wordCount, 2);

                    totalwordPower += wordPower;

                    _resultArea.append("Document Vector : " + wordPower+ newline);

                    similarity = (totalWords * queryVector)
                            / ((Math.sqrt((totalwordPower)) * (Math
                                    .sqrt(((queryVector * 3))))));


                    similarityScore[a] = similarity;

                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File is not found");
        }
        _resultArea.append("The total query frequency for this file is "
                + totalWords+ newline);
        _resultArea.append("The total document vector : " + totalwordPower+ newline);

        _resultArea.append("The similarity is " + similarity+ newline);
        _resultArea.append("\n"+ newline);

    }
    _resultArea.append(" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  "
                    + "\n"+ newline);

    // Display the similarity score between the query and the document in a
    // sorted form
    _resultArea.append("The resulting similarity score of the query "  +  word2+ newline);
    for (a = 0; a < filename; a++) {

        _resultArea.append("abc" + a + ".txt = " + similarityScore[a]+ newline);

    }
    _resultArea.append("\n"+ newline);


    //Array of sorted similarity score  



    for( int i=0; i<arrays.length; i++){


    Arrays.sort(similarityScore);
    arrays[i][0] =   similarityScore[i] ;         
    }

    for(int row = 0; row<11; row++){
        for( int col=0; col<1; col ++){

            _resultArea.append(arrays[row][col]+"\t"+ newline);
        }
        _resultArea.append("\n"+ newline);
    }
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    content.add(scrollingArea, BorderLayout.CENTER);

    this.setContentPane(content);
    this.setTitle("TextAreaDemo B");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
}


public static void main(String[] args) throws FileNotFoundException {
    JFrame win = new readFile();
    win.setVisible(true);
}
}

嗨,我无法追加这一行

System.out.printf("  Term Frequency =  | %8.4f |",

(double) wordCount / totalCount );

到JTextArea。 我错过了什么?

我试着换到

_resultArea.append("  Term Frequency =  | %8.4f |",

(double) wordCount / totalCount ); 

但无济于事。 :(

printf与println不同,因为前者以与String.format(...)相同的方式格式化String,这就是你如何解决它,在String上调用format:

_resultArea.append(String.format("  Term Frequency =  | %8.4f |", (double) wordCount / totalCount)); 

有关更多信息,请查看String APIFormatter API

另外,如果您将来有类似的问题,请不要粘贴这么多代码,因为您上面发布的大多数代码与手头的问题完全无关。 发布的最佳代码是SSCCE,此链接将告诉您所有相关信息: SSCCE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM