I am trying to create a program the checks the frequency of letters in a string but when i run the program it doesn't work right? Any suggestions for how i can fix this?

import java.util.*;

public class LetterFreq{
  public static void main(String[] args){
    String[] lines = new String[50];
    Scanner scan = new Scanner(System.in);
    int pos = 0;
    String t = " ";

    //Gets the text used
    System.out.print("Type text here: ");   
    String wal = scan.next().trim();   
    char [] c = wal.toCharArray();   

    for(int i=0;i<c.length;i++) 
      for(int j=i+1;j<c.length;j++) 
      if(c[i] == c[j]) c[j] = '-';   

    for(int i=0;i<c.length;i++){   
      if(c[i]!='-'){   
        pos++;                               
        t+=String.valueOf(c[i]);   
      }   
    }   

    int f[] = new int[pos];   

    for(int i=0;i<t.length();i++){   
      for(int j=0;j<c.length;j++){   
        System.out.println(c[j]+" "+t.charAt(i));   
        if(wal.charAt(j)==t.charAt(i)) f[i]++;   
      }   
    }   

    //prints the results
    for(int i=0;i<t.length();i++) 
      System.out.println(t.charAt(i)+" "+f[i]);   

  }   
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

There are a lot of places on this forum that have a solution to this. Please make sure you search before posting.

For your convenience, here is one way of doing it.

public int getCount(String toCheck, char interest) 
{
	return toCheck.replaceAll("[^"+interest"]","").length();	
}

Also please make sure to surround code with the code tags

This article has been dead for over six months. Start a new discussion instead.