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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:08:37+00:00 2026-06-14T03:08:37+00:00

now I’ll try to explain what I need to do. I have file.txt file,

  • 0

now I’ll try to explain what I need to do.
I have file.txt file, It looks like:

John //first line - name
One
Three
Four

Peter //first line - name
Two
Three

Elisa //first line - name
One
Three

Albert //first line - name
One
Three
Four

Nicole //first line - name
Two
Four

So I have program’s code:

 public class Testing {


        public static void main(String args[]) throws Exception {
            Scanner input = new Scanner(System.in);
            System.out.println("Select word from list:");
            System.out.println();

            try {
                FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
                BufferedReader br = new BufferedReader(fr);
                String s;
                while((s = br.readLine()) != null) {
                    System.out.println(s);
                }
                fr.close();
                String stilius = input.nextLine();   // eneter word which I want to count in File.txt
                BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before

                int counter = 0;                
                String line;

                System.out.println("Looking for information");
                while (( line = bf.readLine()) != null){
                    int indexfound = line.indexOf(stilius);
                    if (indexfound > -1) {
                         counter++;
                    }

                }
                if (counter > 0) {
                    System.out.println("Word are repeated "+ counter + "times");}
                    else {
                    System.out.println("Error...");
                }
                bf.close(); 

            }
            catch (IOException e) {
                System.out.println("Error:" + e.toString());
                }
            }
        }

This program counting specific word (entered by keyboard) in file.txt.

I need to make this program: for ex.: if I enter word: One It must show:

Word One repeated 3 times by John, Elisa, Albert

All what I need to elect by who this word repeated. But I don’t know really how to make It, maybe LinkedList or I dont know, someone could help me?

Thank you very much.

  • 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-14T03:08:38+00:00Added an answer on June 14, 2026 at 3:08 am

    You can have a List<String> that holds where your word was repeated, and add elements to it on the fly.

    To add elements to it you can use an extra variable lastName (of type String), and in your while loop, do something like:

    if (line.trim().length() == 0) lastName = null; 
    else if (lastName == null) lastName = line;
    

    The first line if for “resetting” the lastName variable, after you changes the name of the name (assuming there is an empty line after all the words of each name)

    The second line is setting it back to the new name after an empty line.

    And in addition when you increase counter do:

    myList.add(lastName)
    

    So the loop will be something like that:

    List<String> resultList = new ArrayList<String>();
    String lastName = null;
    while (( line = bf.readLine()) != null){
       if (line.trim().length() == 0) lastName = null;
       else if (lastName == null) lastName = line;
       int indexfound = line.indexOf(stilius);
       if (indexfound > -1) {
          counter++;
          resultList.add(lastName);
       }
    //do something with resultList, which contains the names
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now I have a long query and I'd like to alternate with such a
Now this is a weird one We have a project where we are reading
Now I have a file with many data in it. And I know the
Now I have routing set up like this. resources :communities do resources :community_topics end
Now I have a http response from website A, I need to change all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Now facebook require to have these options when you want to use bulit-in actions
Now I try to developing an application related to Image processing, for that I
Now I am creating an address book in Java 1.6. Now how I have
Now I'm trying to display the result of consuming a webservice from one page

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.