【问题标题】:Java search text file for string only using charAt() and length()Java 仅使用 charAt() 和 length() 在文本文件中搜索字符串
【发布时间】:2016-03-04 19:22:43
【问题描述】:

我在完成这项任务时遇到了一些麻烦。我相信我已经掌握了搜索方法的基础,但是我的代码中有一个错误导致它无法正常工作。您必须通过扫描仪方法输入文件,并且只允许通过 charAt() 和长度搜索字符串,不允许使用 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 语句,在该语句中,我正在检查由输入文件创建的名为“line”的字符串与 l 输入的名为 word 的字符串。它会产生一个超出范围的错误,数字为 4,但这让我感到困惑,因为在名为 line 的字符串中肯定有超过 4 个字符。我可以更改 if 语句以产生实际结果而不会出现此错误,但我的变量都没有增加其值并保持为 0,仍然证明我的 if 语句不起作用。

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

【问题讨论】:

  • 如果您编写 ypur 代码有什么问题,它可以帮助我们 - 输入示例、所需输出和您的输出/错误。
  • 是的,请提供您的错误日志
  • 欢迎来到 StackOverflow。 “这是我的代码,请找到我的问题”形式的问题被认为是题外话。你甚至没有描述问题是什么。您应该将问题缩小到一个特定的问题,主要是通过单步执行代码来确定行为与您的期望不匹配的地方。一个好的问题陈述的例子是:“当我到达some java code 行时,变量x 的值是y,但我期望z 请访问help center 以及阅读How to Ask
  • 感谢吉姆的建议,我对我的帖子进行了编辑以描述我收到的错误。有什么办法可以让我的帖子回到顶部以便我得到帮助?

标签: java computer-science


【解决方案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;
                    }

                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-09-24
    • 2013-11-09
    • 2015-11-02
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多