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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:25:02+00:00 2026-05-28T01:25:02+00:00

Lets say I have a string = hello. how do i open a text

  • 0

Lets say I have a string = “hello”. how do i open a text file and check if hello exists in that text file?

contents of the text file:

hello:man:yeah

i tried using the code below. Is it file reader only reads the first line? i need it to check all lines to see if hello exists, and then if it does, take “man” from it.

try {
    BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
} catch (IOException e) {
    System.out.println("Error.");
}
  • 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-28T01:25:03+00:00Added an answer on May 28, 2026 at 1:25 am

    If hello:man:yeah is one line in your file, then your code is working right. readLine() will read a line until a newline is found (one line in this case).

    If you just want to see if it’s in the file, then you could do something like this:

     String str;
     boolean found = false;
     while ((str = in.readLine()) != null) {
           if(str != null && !found){
             found = str.contains("hello") ? true : false;
           }
        }
    

    If you need to do a whole word search, you’ll need to use a regular expression. Surrounding your search text with \b will do the whole word search. Here’s a snippet (Note, StringUtils comes from Apache Commons Lang):

        List<String> tokens = new ArrayList<String>();
        tokens.add("hello");
    
        String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(text);
    
        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    

    Of course, if you don’t have multiple tokens, you can just do this:

    String patternString = "\\bhello\\b";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an asp.net label that has the value: Label1.Text = Hello
Let's say that I have this string: s = '<p>Hello!</p>' When I pass this
Lets say I have a string that represents a date that looks like this:
Lets say that I have 10,000 regexes and one string and I want to
Let's say I have the string: hello world; some random text; foo; How could
Hello Lets say I have a console application, that looks like this class Program
Ok lets say I have a File called File.txt and it contains Hello World
Lets say I have two strings: string s1 = hello; string s2 = hello
Let's say I have the string: Hello world; some random text; foo; bla bla;
Let's say I have this string: Hello &, how are you? I'm fine! Is

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.