-2

我从来没有将输出保存到文件中,而且我不知道该怎么做,所以如果有人可以帮助我,那就太棒了。如果需要,代码发布在下面。

代码:

import java.util.*;
import java.io.*;

public class Wordcount {


    public static void main(String[] args) {
        int vowels=0;
        int punctuation=0;
        int sentences=0;
        int words=0;
        int lines=0;
        int alphaNumeric=0;

       try{
       //creates scanner that reads file name    
       Scanner input = new Scanner(System.in);
       System.out.println("Enter file name: ");

       //creates file object of file inputted above
       File file = new File(input.nextLine());
       //if statement for when the file is empty
       if (file.length()==0){
           System.out.println("The input file is empty.");
           System.exit(1);
       }
       //scanner made for reading words of file
       Scanner fileReader = new Scanner(file);
       //new scanner is created to specifically assist in counting the 
       //number of lines in the file
       Scanner lineCounter = new Scanner(file);
       //while loop to count the lines
       while (lineCounter.hasNextLine()) {
           lines++;
           lineCounter.nextLine();
       }

       //while loop that is created to examine each word of the file 
       while(fileReader.hasNext()){
           String word = fileReader.next();
           //for loop which examines each character of each word in the file
           //characters are stored in temp variable ch
           for (int i=0; i<word.length();i++){
               char ch= word.charAt(i);
               //checks for vowels
               if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
                   vowels ++;
               //checks for sentences
               if((ch=='!'||ch=='.'||ch=='?'))
                   sentences ++;
               //checks for alphanumerical character
               if(Character.isLetterOrDigit(ch))
                   alphaNumeric ++;
               //checks for punctuation
               switch(ch){
                   case ',': case '[': case ']': case ':': case '`': case '-':
                   case '!': case '_': case '(': case ')': case '.': case '?':
                   case '"': case ';':
                    punctuation ++;
                    break;

               }    
           }
           //increments the words and then goes back into the loop
           words ++;

       }
       //output
        System.out.println("The number of words in the file name: " + words);
        System.out.println("The number of lines in the file name: " + lines);
        System.out.println("The number of alphanumeric characters "
                + "in the file name: " + alphaNumeric);
        System.out.println("The number of sentences in the file name: "
                + sentences);
        System.out.println("The number of vowels in the file name: " + vowels);
        System.out.println("The number of punctuations in the file name: " 
                + punctuation);

       }catch(FileNotFoundException e){
           e.printStackTrace();
       }
    }
}
4

1 回答 1

0

Unix系统:java MyClass > output.txt

于 2013-02-12T02:51:03.797 回答