-1

I have written a code to find the frequency of each letter of the alphabet in a txt file in Java and write the results in another file .The problem is that frekuenca(String text) has an error cause it must return a result of type String.Can you think of a way to solve it? Thank you!

public class Frekuenca_File {

    public static void main(String args[]) throws FileNotFoundException {
        Scanner input = new Scanner(new File("teksti.txt"));
        PrintStream output = new PrintStream(new File("frekuenca.txt"));
        while (input.hasNextLine()) {
            String fjala = input.next();
            output.print(frekuenca(fjala));
        }
    }

    public static String frekuenca(String text) {
        int frekuenca = 0;
        for (int i = 'a'; i <= 'Z'; i++) {
            for (int j = 0; j < text.length() - 1; j++) {
                if (i == text.charAt(j)) {
                    frekuenca++;
                }
            }
            String result = "" + i + ":" + frekuenca;
            return result;
        }

    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
Malvina
  • 23
  • 6
  • Have it return an empty String at the end? (The method should never reach the end) – GBlodgett Jun 04 '18 at 22:36
  • I believe there must be something wrong with the algorithm also cause I did what you said but it still wont print anything at the other file – Malvina Jun 04 '18 at 22:38
  • 3
    Why do you even return the frequency as a `String`? Why not an `int[]` or a `Map` or `Map`? – Turing85 Jun 04 '18 at 22:38
  • Well you have it return at the end of the inner `for` loop. Why have the outer for loop? It'll never have more than one iteration since you return `result` – GBlodgett Jun 04 '18 at 22:40
  • how can i change the return? Cause the idea was the i would check all alphabet letters with the first for loop and with the second one I would check the characters of the words. If therewas a match the counter would go up. That's why i return at the end of the inner for loop cause I need a result for every letter of the alphabet – Malvina Jun 04 '18 at 22:43
  • You probably want a variable `result` declared outside the outer `for` loop. You can then append to it within the loop, and return it after the outer loop. – Dawood ibn Kareem Jun 04 '18 at 22:43
  • If you want a result for every letter of the alphabet then you should probably be using a `Map` – GBlodgett Jun 04 '18 at 22:44
  • Can't use that cause we havent done that in class yet and we should only work with the knowledge we have so far – Malvina Jun 04 '18 at 22:45
  • What have you been learning in class? – GBlodgett Jun 04 '18 at 22:49
  • Methods,Class,Arrays,GUI – Malvina Jun 04 '18 at 22:50
  • 2
    `for (int i = 'a'; i <= 'Z'; i++)` will perform exactly **zero** loops because `i`, set initially to `'a'`, is already greater than `'Z'` before the first iteration. You might want to have a look at an [ASCII code chart](https://www.asciitable.com/) (also valid for Unicode values `'\u0000'` thru `'\u007f'`); lower-case letters are numerically greater than lower-case. And not all the characters `A`..'z' are letters, either. – Kevin Anderson Jun 04 '18 at 22:57
  • I changed the code to this but it still wont work. `for (int i = 65; i <= 122; i++) { for (int j=0; j – Malvina Jun 04 '18 at 23:00

4 Answers4

0

So a here's what I did:

static int[] counts = new int[26]; {{
       Arrays.fill(counts, 0);  //Fill the array with zeroes
}}
static String alph = "abcdefghijklmnopqrstuvwxyz";     

    public static void main(String[]args) throws IOException {
        Scanner input = new Scanner(new File("input.txt"));
        PrintStream output = new PrintStream(new File("output.txt"));
        while (input.hasNextLine()) {
            String fjala = input.next();
            countLetters(fjala);
        }
        for(int i =0; i < counts.length; i++) {
            output.println(alph.charAt(i) + " : " + counts[i]);
        }
    }
    public static void countLetters(String text) {

        for (int i = 0; i < alph.length(); i++) {
            for (int j = 0; j < text.length(); j++) {
                if (alph.charAt(i) == text.charAt(j)) {
                    counts[i]++;
                }
            }
        }
    }

Logic:

Create a static int array to keep track of how many times each letter occurs. Have a String with all the letters and the use charAt() to move through all the letters. At the end, print all the corresponding letters and letter counts to the file.

Let me know if you have any questions!


Input File:

aaaa
bb
cc
ddddd
eee
fff
j
ddd

Output File:

 a : 4
 b : 2
 c : 2
 d : 8
 e : 3
 f : 3
 g : 0
 h : 0
 i : 0
 j : 1
 k : 0
 l : 0
 m : 0
 n : 0
 o : 0
 p : 0
 q : 0
 r : 0
 s : 0
 t : 0
 u : 0
 v : 0
 w : 0
 x : 0
 y : 0
 z : 0
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
0

To answer your question you can always return a null String object. However I would approach the problem differently. I would parse the text file into chars. Store the number of occurrences of the 26 letters in an array of size 26 (0 - 25). Index 0 is number of a's etc. Each char is an 8 bit number. For example a is 97. (Look up ASCII Table). so the index of the array to be incremented is the char (ASCII value) subtracted by 97. So a 97 - 97 is index 0. b is 98 - 97 is index 1.

-1

create a simple string function like this:

public static void main (String[] args) {
    String output = make_string("tyrone", 5);
    System.out.println(output);     
}

public static String make_string(String string, int x) {
    String return_string = (string + ":" + x);
    return return_string;
}
mikey_nic
  • 1
  • 1
-1

in the function frekuenca, in the for loop ....i="a"and i<"z" ,this causing error i is integer and comparing with character so simply do typecasting or use ascii values in for loop or you can use charAt() function:for (int i = (int)a; i <= (int)Z; i++) char a='A'char z='Z' when storing the result variable 'result' is string and i is an integer you cannot store integer in a string.....do typecasting of i first to character : String result = "" + (char)i + ":" + frekuenca;

see the link

How to count frequency of characters in a string?

https://www.geeksforgeeks.org/print-characters-frequencies-order-occurrence/

Convert int to char in java

SKB
  • 771
  • 7
  • 14
  • 1
    It's not using `String` literals `"a"` and `"Z"` (with double-quotes), but rather _char_ literals `'a'` and `'Z'` (with single quotes). `char` values _are_ integers – Kevin Anderson Jun 04 '18 at 22:55
  • I changed it to this but it still wont print to output file `for (int i = 65; i <= 122; i++) { for (int j=0; j – Malvina Jun 04 '18 at 22:57
  • Sorry, this is entirely wrong. The original code has `char` literals, not `String` literals. And it's perfectly OK to compare `int` values with `char` values without any explicit typecasting, but possibly misleading. – Dawood ibn Kareem Jun 04 '18 at 23:03
  • Yeah but still it doesnt seem to work. This is the output 53:053:053:053:053:0 – Malvina Jun 04 '18 at 23:04
  • To avoid confusion between `char` and `int`, it might have been better to write `for (char letter = 'A'; letter <= 'z'; letter++) {` - note that the upper case letters come before the lower case ones. – Dawood ibn Kareem Jun 04 '18 at 23:05
  • Did you move the `return` to outside the outer loop, as I suggested in my comment under the question? – Dawood ibn Kareem Jun 04 '18 at 23:07