0

I need to take multiple lines of text and count the occurrences of each letter in every line. The last line must end in a period. I'm asked to make an int array of length 26 where arrayName[0] = number of a's, arrayName[1] = number of b's, etc and that letter case needs to be ignored. I'm having trouble checking the occurrences of each letter and defining the indexed variables with the correct number of occurrences. My code so far:

import java.util.Scanner;

public class LetterCounter 
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);

    String[] textBlock = new String[10];

    System.out.println("Enter text.");
    int index;
    //Allows me to type in something for all "lines" of text until I end one with a period.
    for(index = 0; index < textBlock.length; index++)
    {
        textBlock[index] = input.nextLine();

        if(textBlock[index].contains("."))
        {
            System.out.println("Period found");
            break;
        }
    }
    //Outputs what the user printed except for the lines not typed in (they return null).
    System.out.println("Text input by user:");
    for(index = 0; index < textBlock.length; index++)
    {
        if(textBlock[index] != null)
        {
            System.out.println(textBlock[index]);
        }
    }
    //int array
    int[] occurrences = new int[26];

    //used to turn letter into number
    char letter = 'a' - 49;
    char letter2 = 'b' - 49;
    char letter3 = 'c' - 49;

    //checks that numbers are correct (return "0" for a, "1" for b, and "2" for c)
    System.out.println("Number value of a: " + letter);
    System.out.println("Number value of b: " + letter2);
    System.out.println("Number value of c: " + letter3);

}// End of main
}//End of program
Jarvis
  • 8,494
  • 3
  • 27
  • 58
Gatekeeper15
  • 3
  • 1
  • 3
  • For starters, I think you want to take the declaration of the array outside your loop. You probably want to force each letter you read lower case. Then after checking the letter in in "a' to 'z', ++ the array at index of the read letter - 'a'. – Jeremy Kahan Nov 27 '16 at 03:46
  • Similar question here: http://stackoverflow.com/questions/33770180/how-do-i-store-these-printed-letters-into-an-array/33770240#33770240 – markspace Nov 27 '16 at 03:49

3 Answers3

2

Count the character occurrence of every line. and just print them. try this...

//final int SIZE = 1000;
//String[] textBlock = new String[SIZE];

Scanner in = new Scanner(System.in);
String line;

//frequency array for storing which Character is occurs how many times
int[] frequencyArray = new int[26];
Arrays.fill(frequencyArray, 0);

System.out.println("Enter text : ");

// take input un-till find the (.) in a line
// and also count the frequency of Character of current line
while (true) {
    line = in.nextLine();
    for (int i = 0; i < line.length(); i++) {
        char ch = Character.toLowerCase(line.charAt(i));
        if (Character.isLetter(ch)) {
            frequencyArray[ch - 'a']++;
        }
    }
    if (line.contains(".")) {
        break;
    }
}

for (int i = 0; i < 26; i++) {
    System.out.println("Total Number of " + (char)(i + 'a') + " : " + frequencyArray[i]);
}
rimonmostafiz
  • 1,341
  • 1
  • 15
  • 33
0

Declare the array outside your loop and increment the count of that array when the character mathes

Declare this inside the loop

int intialCount = occurrences(charAt(textBlock[index])-49);
occurrences(charAt(textBlock[index])-49) = intialCount++;

You will find occurences of 'a' in occurences[0] which will return a count.

Ajaykumar
  • 416
  • 3
  • 16
0

If you don't print debug messages, you can do the work with only 1 line:

System.out.println("Enter lines of text (period to end):");
new Scanner(System.in).useDelimiter("\\.").next().chars()
  .filter(i -> i >= 'a' && i <= 'c').boxed()
  .collect(Collectors.groupingBy(i -> i, Collectors.counting())
  .forEach((c, n) -> System.out.format("Number value of %s: %d\n", (char)c, n));
Bohemian
  • 412,405
  • 93
  • 575
  • 722