package compressor;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Compressor.java
{
public static void main(String[] args)throws Exception
{
String output="";
Map<String, Integer> map = new HashMap<>();
File file = new File("C:\\Users\\owner\\Desktop\\Compressor\\src\\compressor\\sample.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
String[] array;
int cnt=0;
while ((st = br.readLine()) != null) {
array=new String[st.length()];
array=st.split(" |\\."); /// split string as per [space] or [.] //here |\\ is used for regex OR
for(int i=0;i<array.length;i++){
String cntString=Integer.toString(cnt); /// convert int cnt to String cntString
if(cntString.length()+1 <array[i].length()){
if(map.get(array[i])==null){
map.putIfAbsent(array[i], cnt); //put key along with value in map
output+=array[i]+" ";
}
else{ /// key already present in map hence just withdraw value and add to output
output+="$"+map.get(array[i])+" ";
}
cnt++;
}
else /// for string like * , { ( } )
{
output+=array[i]+" ";
//cnt++;
}
}//for
output+="\n";
}//while
System.out.println(map);
System.out.println("output = \n"+output);
}//main
}