import java.io.BufferedReader ;
import java.io.File ;
import java.io.FileReader ;
import java.util.Iterator ;
import java.util.Set ;
import java.util.Map ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.Scanner ;
class CountChars {
static StringBuilder strb = new StringBuilder();
static int total = 0;
static String SOUGHT;
public static void main (String[] args){
Scanner scanner = new Scanner(System.in) ;
System.out.println("\n\nInput your search . . . \n") ;
String fromConsole = scanner.nextLine() ;
if(fromConsole.length()==0){System.exit(0);}
SOUGHT = fromConsole.toString() ;
try{
File file = new File("C:/EE_Q_CODE/PrideAndPrejudice.txt");
//File file = new File("C:/EE_Q_CODE/TheBible.txt");
//File file = new File("C:/EE_Q_CODE/WarAndPeace.txt");
//File file = new File("C:/EE_Q_CODE/Leviathan.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
Set <Integer> chS = new HashSet<>();
Map <Character, Integer> count = new HashMap<>();
int c;
while(! ((c=(fr.read()))==-1)){
if(!chS.contains((int)c)){chS.add((int)c);count.put((char)c,0);}
int i = count.get((char)c);
if(chS.contains((int)c)){count.replace((char)c,i,i+1);}
builder(c);
}
Iterator it = chS.iterator();
while(it.hasNext()){System.out.print((char)(((Integer)it.next()).intValue())+" ");}
System.out.println();
System.out.println("\nTotal characters processed : "+total);
System.out.println();
/* Alternative output method. (I prefer forEach below).
count.entrySet().stream().forEach(e -> {
System.out.format("key: %s, value: %d%n", e.getKey(), e.getValue());
});
*/
count.forEach((k, v) -> { System.out.format("key: %s, value: %d%n ", k, v); });
}catch(Exception e){e.printStackTrace();}
SOUGHT = null;
fromConsole = null;
System.gc();
}
static void builder(int ch){
int len = strb.length();
total++;
if((char)ch==SOUGHT.charAt(0)){strb.append((char)ch);return;}
if(len>SOUGHT.length()-1){strb.delete(0,strb.length());return;}
if ((char)ch==SOUGHT.charAt(len)&&SOUGHT.charAt(len-1)==strb.charAt(len-1)){strb.append((char)ch);}
else{strb.delete(0,strb.length());return;}
if(strb.toString().equals(SOUGHT)){System.out.printf("%n\"%s\"%s%d%n",SOUGHT," <<< found at position ",total);strb.delete(0,strb.length());}
}
}
builder() is the method thatdoes the searching and reports the position - (and the String).ASKER
You need to move that out of there and make it the last line before end while loopif (chS.contains((int) c)) { count.replace((char) c, i, i + 1); total++; }
ASKER
ASKER
ASKER
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
is being treated as an offset into the file. The problem could be that it's only being incremented conditionally in your while block and not as a matter of course