Reputation: 29
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
Upvotes: 2
Views: 73
Reputation: 8598
You need a rubber duck, and you need to read this: What is a debugger and how can it help me diagnose problems?
You are reading each character individually with (c = in.read()
.
Then you convert the integer (ASCII) value of that character into a String, so for example the first character J
, which has the ASCII value 74, is turned into the String "74".
Then you do some convoluted stuff on that String "74" and replace '7' with '*', giving you "*4".
You do that with every single character, thus creating the resulting output file full of numbers and *.
You need to completely rewrite your code. Here's my suggestion for a proper implementation:
Read the input file in full into one String.
Iterate over the characters in that String to find the character that occurs most often.
Replace that character in your String with asterisks.
Write the resulting String to your output file.
Upvotes: 0
Reputation: 159086
in.read()
returns a single character, i.e. a char
value, but as an int
value to support the extra -1
to indicate end of stream.
Integer.toString(c)
then converts that numeric value as a string.
The numeric value of the first character J
is 74
.
Replace Integer.toString(c)
with Character.toString((char) c)
Upvotes: 2