如何在单独的方法中读取和处理文件,并在Java中的main方法中调用它?

时间:2015-10-22 20:51:19

标签: java arraylist try-catch

我有这个代码,我正在努力计算从.txt文件中读取的给定文本的ARI。这段代码完美无缺,但我希望在main方法中添加更少的东西。如何将try和catch块放在一个新方法中,然后将其调用到我的main方法而不是将所有内容混合在一起?我尝试了一些方法,但我没有得到通常的输出。

这是我的主要课程:

public class MyClass {
public static void main(String[] args){
    String EachSentence;
    int wordCount;

    List<Sentence> sentences = new ArrayList<>();
    List<Integer> EachWordCount = new ArrayList<>();
    List<Integer> EachLetterCounted= new ArrayList<>();

    try{
        File file = new File("a2b.txt");
        Scanner scanner = new Scanner(file);
        scanner.useDelimiter("[.?!]");
        int SentenceCount =0;
        while(scanner.hasNext()){
            SentenceCount++;
            EachSentence = scanner.next();
            EachSentence = EachSentence.replaceAll("\\r?\\n", " ");
            EachSentence = EachSentence.trim();
            if (sentences.add(new Sentence(EachSentence)) && sentences.size() > 1){
                System.out.println("(" + SentenceCount + ") " + EachSentence);
            }
            StringTokenizer tokenizer = new StringTokenizer(EachSentence, " ");
            wordCount = tokenizer.countTokens();
            if(EachWordCount.add(wordCount)&& EachWordCount.size() > 1){
                //I removed the element at position 0 so as to correlate with EachSentence then added the counts to the arrayList
                //to prepare them for addition
                EachWordCount.remove(0);
                EachWordCount.add(wordCount);
            }
            int LetterCount=1;
            for(int i=1; i<EachSentence.length(); i++){
                char currentChar = EachSentence.charAt(i);
                if(currentChar != ' '&& currentChar!='('&&currentChar!=')'&&currentChar!=','&& currentChar!='.'){
                    EachLetterCounted.add(LetterCount);
                    LetterCount++;
                }
            }
        }
        scanner.close();
    }catch (IOException ex){ 
        System.out.println(ex.getMessage());
    }


    //Computes the ARI of the total sentences
    double lettersCounted = (double) lettersCounted(EachLetterCounted);
    double wordsCounted = (double) sum(EachWordCount);
    double sentencesCounted = (double) SentencesCounted(EachWordCount);
    double AutomatedReadabilityIndex= 4.71*(lettersCounted/wordsCounted) + 0.5 * (wordsCounted/sentencesCounted) -21.43;

    System.out.println("\nSummary statistics: ");
    System.out.println("Letters: "+lettersCounted(EachLetterCounted));

    System.out.println("Words: "+ sum(EachWordCount));
    System.out.println("Sentences: " + SentencesCounted(EachWordCount));

    DecimalFormat df = new DecimalFormat("#.##");
    System.out.println("readability: "+ df.format(AutomatedReadabilityIndex));
}

捐款会很棒!

1 个答案:

答案 0 :(得分:0)

第二次回答我的问题。(对于任何有同样问题的人)我通过简单地将main方法中的所有内容放在...static void ReadFile()方法中并在main方法中只调用`ReadFile来实现这一点。它可能不是最好的方式,但它正是我所寻找的:)

public static void main(String[] args) {
    ReadFile();
}

public static void ReadFile() {
   //insert all what is in main method, the exceptions were handled
   //here also. So there was no need for the throws IOException that 
   //should've followed.
}

OR

public static void main(String[] args) {
    ReadFile(//insert name of file to be read here);
}

public static void ReadFile(String filename) {
   //insert all what is in main method, the exceptions were handled
   //here also. So there was no need for the throws IOException that 
   //should've followed.
}