Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8003123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:34:21+00:00 2026-06-04T16:34:21+00:00

I am trying to learn about searching for certain keywords for a project I

  • 0

I am trying to learn about searching for certain keywords for a project I am attempting. I am using a program created by someone else and I have tried to modify it to accept two arguments. I have so far attempted to add args[1] after the “Searching for ” + args[0] in hope that it would search for the second argument however this has not worked and lead to the program reporting that the position was found at -1 of line 2. The text file I am reading from displays as:

one
two
three

Can anyone provide help to how I can pass in two arguments.

Thank you

// Import io so we can use file objects
import java.io.*;

public class searchfile {
public static void main(String args[]) {
    try {
        // Open the file c:\test.txt as a buffered reader
        BufferedReader bf = new BufferedReader(new FileReader("C:/Users/Sean/Desktop/Java/MyText.txt"));

        // Start a line count and declare a string to hold our current line.
        int linecount = 0;
            String line;

        // Let the user know what we are searching for
        System.out.println("Searching for " + args[0] + " in file...");

        // Loop through each line, stashing the line into our line variable.
        while (( line = bf.readLine()) != null)
        {
                // Increment the count and find the index of the word
                linecount++;
                int indexfound = line.indexOf(args[0]);

                // If greater than -1, means we found the word
                if (indexfound > -1) {
                     System.out.println("Word was found at position " + indexfound +" on line " + linecount);
                }
        }

        // Close the file after done searching
        bf.close();
    }
    catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:34:22+00:00Added an answer on June 4, 2026 at 4:34 pm
    // Import io so we can use file objects
    import java.io.*;
    
    public class searchfile {
    public static void main(String args[]) {
        try {
            // Open the file c:\test.txt as a buffered reader
            BufferedReader bf = new BufferedReader(new FileReader("C:/Users/Sean/Desktop/Java/MyText.txt"));
    
            // Start a line count and declare a string to hold our current line.
            int linecount = 0;
                String line;
    
            // Let the user know what we are searching for
            // i.e. just printing to the console, not actually searching.
            System.out.println("Searching for " + args[0] + " in file...");
    
            // Loop through each line, stashing the line into our line variable.
            while (( line = bf.readLine()) != null)
            {
                    // Count lines. We read a line this variable (linecount) increments by one
                    linecount++; 
                    // the indexOf function returns the index (i.e. the place of) the parameter, in this case args[0]. If it doesn't find the parameter, it returns -1, an impossible value, so we know it wasn't found.
                    // Here is the actual searching done, till...
                    int indexfound = line.indexOf(args[0]);
    
                    // If greater than -1, means we found the word
                    if (indexfound > -1) {
                         System.out.println("Word was found at position " + indexfound +" on line " + linecount);
                    }
                    // ...here.
            }
    
            // Close the file after done searching
            bf.close();
        }
        catch (IOException e) {
            System.out.println("IO Error Occurred: " + e.toString());
        }
    }
    
    }
    

    I said in comments where the searching is done. I think you can copy most of that and do that again for args[1].

    For more arguments (not sure if it’s the best/fastest solution):

    // Import io so we can use file objects
    import java.io.*;
    
    public class searchfile {
    public static void main(String args[]) {
        try {
            // Open the file c:\test.txt as a buffered reader
            BufferedReader bf = new BufferedReader(new FileReader("C:/Users/Sean/Desktop/Java/MyText.txt"));
    
            // Start a line count and declare a string to hold our current line.
            int linecount = 0;
                String line;
    
            // Let the user know what we are searching for
            // i.e. just printing to the console, not actually searching.
            System.out.println("Searching for " + args[0] + " in file...");
    
            // Loop through each line, stashing the line into our line variable.
            while (( line = bf.readLine()) != null)
            {
                    // Count lines. We read a line this variable (linecount) increments by one
                    linecount++; 
                    // the indexOf function returns the index (i.e. the place of) the parameter, in this case args[0]. If it doesn't find the parameter, it returns -1, an impossible value, so we know it wasn't found.
                    // Here is the actual searching done, till...
                    for(int index = 0; true; index++) {
                    try {
                    int indexfound = line.indexOf(args[index]);
    
                    // If greater than -1, means we found the word
                    if (indexfound > -1) {
                         System.out.println("Word was found at position " + indexfound +" on line " + linecount);
                    }
                    }
                    catch(ArrayIndexOutOfBoundsException ex) {
                        break;
                    }
                    // ...here.
                    }
            }
    
            // Close the file after done searching
            bf.close();
        }
        catch (IOException e) {
            System.out.println("IO Error Occurred: " + e.toString());
        }
    }
    
    }
    

    I’m not sure if this works, probably not, but I wrote this just out of my head (not tested).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn about Google maps API. I have a project that
I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using
I have trying to learn about resources. In VS when I create the project:
Trying to learn about php's arrays today. I have a set of arrays like
i'm trying to learn about user controls. I created a user control that has
I am trying to learn about dependency injection and i'm using the unity application
I'm trying to learn about device drivers on Linux Kernel, for that I've created
I am trying to learn about simple shape detection for a project I'm working
I'm trying to learn more about 'ajax' and so far have had the most
I have been trying to learn about classes in PHP and part of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.