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 3215142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:08:36+00:00 2026-05-17T15:08:36+00:00

how to search for a certain word in a text file in java? Using

  • 0

how to search for a certain word in a text file in java?
Using buffered reader, I have this code, but I get a

java.lang.ArrayIndexOutOfBoundsException

Please help me determine what’s wrong with my program.

System.out.println("Input name: ");
      String tmp_name=input.nextLine();


        try{

             FileReader fr;
      fr = new FileReader (new File("F:\\names.txt"));
      BufferedReader br = new BufferedReader (fr);
String s;
while ((s = br.readLine()) != null) {

String[] st = s.split(" ");
String idfromtextfile=st[0];
String nemfromtextfile = st[1];
String scorefromtextfile=st[2];


if(nemfromtextfile.equals(tmp_name)){
    System.out.println("found");      
}else{
    System.out.println("not found");
}



      }

  }catch(Exception e){ System.out.println(e);}

names.txt looks like this:

1
a
0

2
b
0
  • 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-05-17T15:08:36+00:00Added an answer on May 17, 2026 at 3:08 pm

    Your code expects each line in the file to have three space-separated words. So your file must look like this:

    1 a 0
    2 b 0
    

    The ArrayIndexOutOfBoundsException occurs if there is a line in the file that does not have three space-separated words. For example, there might be an empty line in your file.

    You could check this in your code like this:

    if ( st.length != 3) {
        System.err.println("The line \"" + s + "\" does not have three space-separated words.");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I search for nurple in a file. I found it, great. But now, every
I want to search a directory for all files that match a certain pattern.
Search the web for the phrase I cosay. I run across this phrase being
Which search engine would you recommend for a Commerce website? We have millions of
@search_results = Array.new duplicates = Set.new results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url]) } This
i search a good tutorial to learn how to use entity framework in the
Binary search is harder to implement than it looks. Although the basic idea of
When implementing a needle search of a haystack in an object-oriented way, you essentially
I want to implement search functionality for a website (assume it is similar to
I'd like to search for a given MAC address on my network, all from

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.