使用四种方法来计算和显示java文件中的行、单词和字符



这是我第一次在程序中使用多个方法,我也从未尝试过计算文件中的行、单词和字母。因此,我阅读了三本Java书籍的文件部分,然后查看了论坛上的问题,并进行了尝试。我认为我在变量名方面做得不对,因为我不知道在多方法等中把它们放在哪里的规则。如果你能说这样的事情是明显错误的,因为你必须这样做,那将是有帮助的。我不能使用调试器,因为我不能编译它。无论如何,谢谢你看这个。

import java.util.Scanner;
    import java.io.*;
    /**
     * A program to count the number of lines, words and characters in a text file.
     * 
     * @author 
     * @version 2014
     */
    public class WordCount
    {
        public static void main(String[] args)  throws FileNotFoundException 
        {
            // get the filename
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Enter the name of the file: ");
            String filename = keyboard.nextLine();
            File file = new File(filename);
            Scanner inputFile = new Scanner(file);
            lines(filename);
            words(filename);
            characters(filename);
        }
        /**
         * Count the number of lines in a file, and print out the result.
         * 
         */
        public static void lines(String filename)  throws FileNotFoundException 
        {
            Scanner inputFile = new Scanner(file);
            int lines = 0;
            while (inputFile.hasNextLine())
            {
                lines++;
                file.nextLine();
            }
            return lines;
            inputFile.close();
        }
        /**
         * Count the number of words in a file, and print out the result.
         *
         */
        public static void words(String filename)  throws FileNotFoundException 
        {
            Scanner inputFile = new Scanner(file);
            int words = 0;
            while (input.hasNextLine())
            {
                String word = input.next();
                words++;
            }
            return words;
            inputFile.close();
        }
        /**
         * Count the number of characters in a file, and print out the result.
         * 
         */
        public static void characters(String filename)  throws FileNotFoundException 
        {
            Scanner inputFile = new Scanner(file);
            int characters = 0;
            while (inputFile.hasNextLine())
            {
                String line = inputFile.nextLine();
                for(int i = 0; i < line.length(); i++)
                { characters = line.charAt(i);
                    if (character != 32) 
                    { 
                        characters++;
                    }
                }
            }
            return characters;
            inputFile.close();
            System.out.println("The number of lines is: " + lines);
            System.out.println("The number of words is: " + words);
            System.out.println("The number of characters is: " + characters);
            System.out.println();
        }
    }

更改以下

public static int lines(String filename)  throws FileNotFoundException 
//change the return type to int instead of void
{
    Scanner inputFile = new Scanner(filename); //filename instead of file
    int lines = 0;
    while (inputFile.hasNextLine())
    {
        lines++;
        inputFile.nextLine(); // ---do----
    }
    inputFile.close(); // after return it will become unreachable
    return lines;
}

同样适用于其他方法

您的代码包含几个问题。因此,让我们在每种方法中逐一解决这个问题:

public static void main(String[] args)  throws FileNotFoundException 
{
    // get the filename
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter the name of the file: ");
    String filename = keyboard.nextLine();
    keyboard.close(); // <- don't forget to close this resource
    File file = new File(filename);
    //Scanner inputFile = new Scanner(file); // <- this line is unnecessary, because you never use that variable
    lines(file); // <- you already obtained a File object, so use it ;)
    words(file);
    characters(file);
}

下一种方法:

public static void lines(File file)  throws FileNotFoundException // mind the new argument type
{
    Scanner inputFile = new Scanner(file);
    int lines = 0;
    while (inputFile.hasNextLine())
    {
        lines++;
        inputFile.nextLine(); //<- use "inputFile" instead of "file"
    }
    inputFile.close();
    //return lines; // <- you can't return something if your method uses "void" as the return type
    //your comment on that method says that you like to print the result, so let's do that
    System.out.println("The number of lines is: " + lines);
}

下一个:

public static void words(File file)  throws FileNotFoundException // <- new argument type
{
    Scanner inputFile = new Scanner(file);
    int words = 0;
    while (inputFile.hasNextLine())
    {
        //String word = inputFile.next(); // that won't work like you think, but we can do a little "trick" here
        String line = inputFile.nextLine(); // read the current line of text
        words += line.split(" ").length; // split the line using whitespace and add the number of words to the current value of variable "words"
        //words++; // <- not needed anymore
    }
    //return words; // <- like before: not possible if return type is void
    inputFile.close();
    //your comment on that method says that you like to print the result, so let's do that
    System.out.println("The number of words is: " + words);
}

最后一种方法:

public static void characters(File file)  throws FileNotFoundException 
{
    Scanner inputFile = new Scanner(file);
    int characters = 0;
    int count = 0; // new variable to count the characters
    while (inputFile.hasNextLine())
    {
        String line = inputFile.nextLine();
        for(int i = 0; i < line.length(); i++)
        {
            characters = line.charAt(i); // <- no harm in using a new line :)
            if (characters != 32) // <- you wrote "character"
            { 
                count++; // <- "characters" is the current character itself, we won't increase that. For example "a" would become "b" and we don't want/need that :)
            }
        }
    }
    //return characters; // <- you know that already ...
    inputFile.close();
    //System.out.println("The number of lines is: " + lines); // <- "lines" and "words" are unknown here, but we printed these results already
    //System.out.println("The number of words is: " + words);
    System.out.println("The number of characters is: " + count); // <- print the count of characters
    System.out.println();
}

在这些更改之后,您的代码现在应该可以工作了:)。

这些方法似乎只需要File对象。

int n = countLines(file);
System.out.println("Lines: " + n);
...
public static int countLines(File textFile)  throws IOException 
{
    Scanner inputFile = new Scanner(textFile);
    int lines = 0
    while (inputFile.hasNextLine())
    {
        lines++;
        inputFile.nextLine();
    }
    inputFile.close();
    return lines;
}

对于下一行,您询问了错误的对象,需要扫描仪。

return从方法返回,所以之前已经接近了。

当您返回行计数时,您必须返回内部

方法名称按约定包含一个动词。

该方法不知道filefilename。如果您传递一个变量(如file),您可以为新的参数赋予相同的名称,但为了清晰起见,此处未进行说明。

问题编号1:

public static void lines(String filename)  throws FileNotFoundException 
{
    Scanner inputFile = new Scanner(file);
    int lines = 0;
    while (inputFile.hasNextLine())
    {
        lines++;
        file.nextLine();
    }
    return lines; // <----------------
    inputFile.close();
 }

这是一个void方法,但是您有一个return lines语句。删除它或将方法标头更改为public static int lines(String filename) throws FileNotFoundException

问题2

public static void words(String filename)  throws FileNotFoundException 
        {
            Scanner inputFile = new Scanner(file);
            int words = 0;
            while (input.hasNextLine())
            {
                String word = input.next();
                words++;
            }
            return words; <-------------
            inputFile.close();
        }

在这里,您再次请求void方法返回一个值。将方法标题更改为public static int words(String filename) throws FileNotFoundException

问题3:

public static void characters(String filename)  throws FileNotFoundException 
        {
            Scanner inputFile = new Scanner(file);
            int characters = 0;
            while (inputFile.hasNextLine())
            {
                String line = inputFile.nextLine();
                for(int i = 0; i < line.length(); i++)
                { characters = line.charAt(i);
                    if (character != 32) 
                    { 
                        characters++;
                    }
                }
            }
            return characters; //<---------------
            inputFile.close();
            System.out.println("The number of lines is: " + lines);
            System.out.println("The number of words is: " + words);
            System.out.println("The number of characters is: " + characters);
            System.out.println();
        }

这里还有一个关于void方法的return int语句。只需将方法标题更改为:public static int characters(String filename) throws FileNotFoundException

最新更新