I'm creating an application that gets input from input.txt and finds the most frequent characters and replaces them with * symbols. But the output.txt displays random numbers with * . So I have no idea what's gone wrong.
import java.io.*;
public class StringTest1 {
public static void main(String[]args)
throws IOException{
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int c;
while ((c = in.read()) != -1) {
String sen = Integer.toString(c);
String str = sen.toLowerCase();
int[] freq = new int[str.length()];
char maxChar = str.charAt(0);
int i, j, max;
//Converts given string into character array
char string[] = str.toCharArray();
//Count each word in given string and store in array freq
for(i = 0; i < string.length; i++) {
freq[i] = 1;
for(j = i+1; j < string.length; j++) {
if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') {
freq[i]++;
//Set string[j] to 0 to avoid printing visited character
string[j] = '0';
}
}
}
//Determine maximum occurring characters
max = freq[0];
for(i = 0; i <freq.length; i++) {
//If max is less than frequency of a character
//then, store frequency in max and corresponding character in maxChar
if(max < freq[i]) {
max = freq[i];
maxChar = string[i];
}
}
String maxLetter = Character.toString(maxChar);
String line = str.replaceAll(maxLetter, "*");
char[]ch = line.toCharArray();
out.write(line);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
*4*7**8*7*2*4*8***7*2*4*3*0*2*3*0***4**8*08*0***6**5*0*3**6**4**7**6**5*2*2*05*8*0***4**0*7**6*0**2*06*7**8*7*2*7*0**8*2*3*0***4**8*05***0***5*0*3**2**4*05**0*03*2*4*3*0*2*4*5*6*5*0*4*04**4*0**7*00**5*2*4*7*6*7*2*7*****0****7**4**4*0***0**6*2*0**4****03**4*7*09*09*05**0*03*0*06*5**8*5*2*2*7*00********2*2*4*00*8***2*06**5*02*0**5**2**4*05**0*03*2*4**5*02*2*06*00*8***2*04*05*8*0***4**0*7**6*0**0
But I expect it to be word with most repeated character changed to *.
The original text (input.txt)
Java JDBC JSP Servlets
Struts Hibernate java Web Services
Spring JSF JAVA
Threads JaVa Concurrent Programming
jAvA Hadoop Jdbc jsf
spring Jsf jdbc hibernate