不能追加行之一的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); 
} 
} 

嗨,我不能添加这一行不能追加行之一的JTextArea

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

(double) wordCount/totalCount); 

到JTextArea中。我错过了什么?

我尝试改变

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

(double) wordCount/totalCount); 

,但无济于事。 :(

printf和println不一样,前者的格式与String.format(...)所做的相同,这就是你如何解决它,调用字符串的格式:

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

更多关于这一点,检查出String APIFormatter API

顺便说一句,如果你有在未来类似的问题,不糊,因为大部分你已经码了这么多的代码上面的帖子与手头的问题完全无关,最好的帖子是SSCCE,这个链接会告诉你所有关于这个的:SSCCE

+0

谢谢先生,我知道我发布的代码是丛林代码。但是,我担心如果我错过了一些小的细节,它无法帮助人们来帮助我。对不起,代码显示不好。我会记住你的建议。谢谢你的帮助。它编译。非常感谢。 – jasper 2011-03-26 15:36:37

+0

这就是为什么最好将问题与SSCCE分开,正如我在上面提供的链接中所讨论的那样。别客气。 – 2011-03-26 15:40:15