-6

My example text:

This-File-Contains-184-Characters. The-Most-Frequent-Letter-Is-"E". The-File-Includes-2-Upper-Case-Occurences And-22-Lower-Case-Occurences-Of-"E". The-Total-Number-Of-Its-Occurences-Is-24.

The example letter I'm using is "e".

My code:

 import java.io.*;
 import java.util.Scanner;

 public class Homework4a
 {
public static void main(String[] args) throws IOException
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter name of the input file: ");
    String fileName = keyboard.nextLine();
    System.out.println("Enter letter: ");
    char letter = keyboard.nextLine().charAt(0);
    File file = new File(fileName);
    Scanner scan = new Scanner(new FileReader(file));
    try
    {

            char lowerCaseLetter = (new Character(letter)).toString().toLowerCase().charAt(0);
            char upperCaseLetter = (new Character(letter)).toString().toUpperCase().charAt(0);
            int lowerCounter=0;
            int upperCounter = 0;

        while(scan.hasNextLine())
        {
            String input = scan.nextLine();

            for(int i=0; i<input.length(); i++)
            {
                if(input.charAt(i)== lowerCaseLetter)
                {
                    lowerCounter++;
                }
                else if(input.charAt(i)== upperCaseLetter)
                {
                    upperCounter++;
                }

            }
        }
            int totalLowerCounter = lowerCounter;
            int totalUpperCounter = upperCounter;
            int totalCounterSum = totalLowerCounter + totalUpperCounter;


            System.out.println("The lower-case letter " + lowerCaseLetter + " occurs " + totalLowerCounter + " times");
            System.out.println("The upper-case letter " + upperCaseLetter + " occurs " + totalUpperCounter + " times");
            System.out.println("The total number of occurrences (\"" + lowerCaseLetter + "\" and \"" + upperCaseLetter +
                        "\") is " + (totalCounterSum));

    }
    finally
    {
        scan.close();
    }
}
 }
  • Possible duplicate of http://stackoverflow.com/questions/27242095/java-count-occurrences-of-characters-in-text-file and http://stackoverflow.com/questions/15021163/counting-letter-occurrence – Robin Topper Apr 12 '17 at 14:00
  • 1
    I don't mean to be rude or disrespectful, but as-is, your question is *too broad*. What is the specific question? I would recommend you this: use *divide and conquer* (that is, split the task up into its smallest pieces and tackle them one by one) and look up the individual requirements. You should find everything you need online (Perform a search on "read text file java" etc). Then write some more code. Once you run into an actual, specific issue, come back here. That said, check out [ask]. – domsson Apr 12 '17 at 14:03
  • 1
    Basically, your question is "can you help me". Please read [Why is “Can someone help me?” not an actual question](https://meta.stackoverflow.com/a/284237/139985). That will help you understand why people are being negative about your Question. – Stephen C Apr 12 '17 at 14:15
  • Darnell, see my answer for some pointers in the right direction. Also, see Stephen C's comment. – domsson Apr 12 '17 at 14:17
  • Basically, there is no actual question in your question right now. You are asking for help. You should try to solve this as best as you can, then ask a *specific* question once you run into a *specific* problem (like *"Reading a file line by line should give 17 lines, only gives 3"*). To get there, I wrote up some general advice for you, see below. Also, you can edit by using the `edit` link right at the bottom of your question. – domsson Apr 12 '17 at 14:28
  • 1
    I still don't see a question. – shmosel Apr 14 '17 at 01:21
  • I don't get why this question hasn't been closed yet. – domsson Jul 24 '17 at 12:17

3 Answers3

0

I'll give you some pointers. My best advice is to use divide & conquer:

  • Get the file name from user input (you already know about Scanner)
  • Read the text file (check out BufferedReader and FileReader)
  • Remember that a char is basically just an int, look up an ASCII table
  • You can therefore use an array (int[]) where the indices are the ASCII values, the values are the number of occurence
  • Go over the contents of the file, char by char, and add to the array accordingly

Basically, divide & conquer is all about splitting a task into its smallest problems, then tackle them individually. By breaking an assignment down in this way, even quite complex problems will come down to small problems that are easy to solve.

What's also nice is that once you've broke it down like that, you can go ahead and write a method for each of these sub-tasks. This way, you get a nicely organized code "for free".

domsson
  • 4,553
  • 2
  • 22
  • 40
0

I figured out the problem, I needed to have the println outside of the loop. This code can help you read a text file and find a specific character by changing the variable in the "if else" statement to the specific character you need to find in the text file. It then calculates how many lowercase and uppercase letter and the total of them both.

-3

Here is the code that u can formulate to find a letter count from a text file.i have pushed an harcoded letter 'a' u can change it to dynamic also.

import java.io.*; import java.util.Scanner;

public class CountTheNumberOfAs {
    public static void main(String[] args)throws IOException
    {

        String fileName = "JavaIntro.txt";
        String line = "";
        Scanner scanner = new Scanner(new FileReader(fileName));
        try {

          while ( scanner.hasNextLine() ){
            line = scanner.nextLine();
            int counter = 0;

            for( int i=0; i<line.length(); i++ ) {
                if( line.charAt(i) == 'a' ) {
                    counter++; 

                } 


            }

             System.out.println(counter);   
          }
        }
        finally {

          scanner.close();


    }}}
Amit Gujarathi
  • 1,090
  • 1
  • 12
  • 25