仅使用charAt()和length()的字符串的Java搜索文本文件

时间:2016-03-04 19:22:43

标签: java computer-science

我对这项任务有点麻烦。我相信我已经掌握了搜索方法的基础,但是我的代码中的某个错误导致它无法正常工作。您必须通过scanner方法输入文件,并且只允许通过charAt()和length搜索字符串,不允许在String或String构建器类中使用其他方法。

我必须在文件中搜索该单词并报告找到单词的次数以及它所在的行号。

感谢任何帮助,谢谢。

public void FindWord(String fileName, String word)throws IOException
{
    Scanner fileInput = new Scanner (new File(fileName));//scanner that reads the file
    int lineCounter = 0; //declares the line
    String line;

    while (fileInput.hasNextLine())
    {
        line = fileInput.nextLine();
        System.out.println(line);
        lineCounter++;
        outerloop: for (int i = 0; i <= line.length();i++)
        {
            for (int j = 0; j <= word.length();)
            {
                if (line.charAt(i) == word.charAt(j))
                {
                    if (j == word.length())
                    {
                        amount++;
                        lineNumbers += (lineCounter + ", ");
                        continue outerloop;
                    }
                    j++;
                    i++;
                }

                else if (line.charAt(j) != word.charAt(j))
                {
                   continue outerloop;
                }

            }
        }
    }
}

编辑:好吧,我确实将其缩小到一个特定的问题。代码一直运行到if语句,我在那里检查名为&#34; line&#34;的字符串。由输入文件创建的字符串l输入名为word。它产生一个越界错误,数字为4,但这让我感到困惑,因为字符串中的字符串肯定超过4个字符。我可以改变if语句以产生没有这个错误的实际结果,但是我的变量都没有增加它们的值并保持为0,仍然证明我的if语句不起作用。

所以基本上,我需要帮助找到一个解决方案来检查行中的字符与单词输入,只使用charAt()和length()方法

1 个答案:

答案 0 :(得分:-1)

我希望,此代码可以解决您的问题。请检查。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Program {

    public static void main(String[] args) throws IOException {
        String fileName = "F:/input.txt";
        String word = "name";
        FindWord(fileName, word);
    }
    public static void FindWord(String fileName, String word)throws IOException
    {
        Scanner fileInput = new Scanner (new File(fileName));//scanner that reads the file
        int lineCounter = 0; //declares the line
        String line;

        while (fileInput.hasNextLine())
        {
            int amount = 0;
            String lineNumbers = "";
            line = fileInput.nextLine();
            System.out.println(line);
            lineCounter++;
            outerloop: for (int i = 0; i <= line.length();i++)
            {
                for (int j = 0; j <= word.length();)
                {
                    if (line.charAt(i) == word.charAt(j))
                    {
                        if (j == word.length())
                        {
                            amount++;
                            lineNumbers += (lineCounter + ", ");
                            continue outerloop;
                        }
                        j++;
                        i++;
                    }

                    else if (line.charAt(j) != word.charAt(j))
                    {
                       continue outerloop;
                    }

                }
            }
        }
    }
}
相关问题