0

I'm having a bit trouble with this assignment. I believe I already have the basis of the searching method covered, but there is an error in my code somewhere that makes it not work. You have to input the file via scanner method and are only allowed to search for the string via charAt() and length, no other methods in the String or String builder class are allowed.

I have to search the file for the word and report back the amount of times the word is found and which line number it is on.

Any help is appreciated, thanks.

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;
                }

            }
        }
    }
}

Edit: well, I did narrow it down to a specific problem. The code runs up until the if statement where I'm checking the string called "line" created by the input file against the string l entered called word. It produces an out of bounds error, with the number 4, but that confuses me because there's definitely more than 4 characters in the string called line. I can alter the if statement to produce real results without this error, but none of my variables have their values increased and remain at 0, still proving that my if statement doesn't work.

So basically, I needed help finding a solution to checking the characters in the line against the word input, only using the charAt() and length()methods

4
  • It can help us if you write whats wrong with ypur code - a example of input, desired output and your output/error. Commented Mar 4, 2016 at 19:25
  • yes please provide your error log Commented Mar 4, 2016 at 19:26
  • 1
    Welcome to StackOverflow. Questions of the form "Here's my code, please find my problem" are considered off-topic. You have not even described what the problem is. You are expected to have narrowed the problem down to a specific question, primarily by stepping through the code to identify where the behavior doesn't match your expectation. An example of a good problem statement would be : "When I get to line some java code variable x has value y but I expected z" Please visit the help center and also read How to Ask. Commented Mar 4, 2016 at 19:26
  • Thanks for the advice Jim, I made an edit on my post to describe the error I was receiving. Is there any way to bump my post back to the top so I can get some help? Commented Mar 5, 2016 at 2:52

1 Answer 1

0

I hope, this code will solve your issue. Please check.

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;
                    }

                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.