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

  • Home
  • SEARCH
  • 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 8257491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:12:53+00:00 2026-06-08T02:12:53+00:00

I have here a program that enters a paragraph and writes it into a

  • 0

I have here a program that enters a paragraph and writes it into a file. After that, it should count the occurrences of each letters (case sensitive). However, it doesn’t count the number of letter occurrences. I think I put the for loop in the wrong place.

import java.io.*;
import java.util.*;
public class Exercise1 {

    public static int countLetters (String line, char alphabet) {
        int count = 0;
        for (int i = 0; i <= line.length()-1; i++) {
            if (line.charAt(i) == alphabet)
                count++;
        }
        return count;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in));
        PrintWriter outputStream = null;
        Scanner input = new Scanner (System.in);
        int total;

        try {
            outputStream = new PrintWriter (new FileOutputStream ("par.txt"));
            System.out.println("How many lines are there in the paragraph you'll enter?");
            int lines = input.nextInt();
            System.out.println("Enter the paragraph: ");
            String paragraph = buffer.readLine();
            outputStream.println(paragraph);
            int j;
            for (j = 1; j<lines; j++) {
                paragraph = buffer.readLine();
                outputStream.println(paragraph);    
            }
            outputStream.close();
            System.out.println("The paragraph is written to par.txt");

            for (int k=1; k<lines; k++) {
                paragraph = buffer.readLine();
                total = countLetters (paragraph, 'A');
                if (total != 0)
                    System.out.println("A: "+total);
                            //I'll do bruteforce here up to lowercase z

            }
        }

        catch(FileNotFoundException e) {
            System.out.println("Error opening the file par.txt");
        }

    }

}

Please help me fix the code. I’m new in programming and I need help. 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-08T02:12:56+00:00Added an answer on June 8, 2026 at 2:12 am

    First, your initial reading user input is a bit of a waste since you read once then enter the for loop for the rest – this is not a problem, just a better code.

    // your code
    String paragraph = buffer.readLine();
    outputStream.println(paragraph);
    int j;
    for (j = 1; j<lines; j++) {
         paragraph = buffer.readLine();
         outputStream.println(paragraph);    
     }
    

    You can just put them in the loop:

    // better code
    String paragraph;
    int j;
    for (j = 0; j<lines; j++) {
         paragraph = buffer.readLine();
         outputStream.println(paragraph);    
    }
    

    Then your first problem comes from the way you read the lines:

    // your code - not working
    outputStream.close();
    for (int k=1; k<lines; k++) {
          paragraph = buffer.readLine();
          total = countLetters (paragraph, 'A');
    

    Consider what happened above:

    • The input is already DONE, the output is already written and stream is closed – up to here everything is good
    • Then when you try to count the number of characters, you do: paragraph = buffer.readLine(); – what does this code do? It waits for another user input (instead of reading what’s been inserted)

    To fix the problem above: you need to read from what’s already been written – not asking for another input. Then instead of brute forcing every character one by one, you can just put them into a list and write a for loop.

    So now, you want to read from the existing file that you already created (ie. reading what WAS inputted by the user):

    BufferedReader fileReader = new BufferedReader(new FileReader(new File("par.txt")));
    
    String allCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String aLineInFile;
    
    // Read the file that was written earlier (whose content comes from user input)
    // This while loop will go through line-by-line in the file
    while((aLineInFile = fileReader.readLine()) != null)
    {
          // For every line in the file, count number of occurrences of characters  
          // This loop goes through every character (a-z and A-Z)  
          for(int i = 0; i < allCharacters.length(); i++)
          {
                // For each single character, check the number of occurrences in the current line
                String charToLookAt = String.valueOf(allCharacters.charAt(i));
                int numOfCharOccurancesInLine = countLetters (aLineInFile, charToLookAt);
    
                System.out.println("For line: " + aLineInFile + ", Character: " + charToLookAt + " appears: " + numOfCharOccurancesInLine + " times " );
          }         
    }
    

    The above gives you the number of occurrences of every character in every line – now you just need to organize them to keep track of how many are in total for the whole file.

    Code-wise, there might be better way to write this to have cleaner implementation, but the above is easy to understand (and I just wrote it very quickly).

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

Sidebar

Related Questions

NOTE: This is a followup to my question here. I have a program that
Well, i have written a simple python program that parses HTML with HTMLParser. Here
I have a program that takes a list of names from a file and
I have here a program that is supposed to begin with an array of
I have a program here that will solve an expression... First I need to
I have a program that searches a file (numbers.dat) using a binary search and
I have a program here that has an entry box and a button. I
I'm at the end of my rope here: I have a single-threaded C++ program.
I have been looking at ways to program a UI in ruby here, on
A slightly archaic question I'm afraid but here goes: I have a program which

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.