i have copy txt file ram(use bytearrayoutputstream), next make list of words , number of repeats. when try print list, print me
"java.util.scanner[delimiters=\p{javawhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\ ][decimal separator=\,][positive prefix=][negative prefix=\q-\e][positive suffix=][negative suffix=][nan string=\q?\e][infinity string=\q?\e]
i found here problem(that scanner not string object)but can't find solution.
could u check code , help? {
public class javaram {
file f; byte [] btab; scanner sc; string templine; bytearrayoutputstream baos; stringtokenizer st; long starttime=0; long stoptime=0; hashmap<string,integer> hm; javaram(string filename) throws ioexception{ starttime=system.currenttimemillis(); loadfile(filename); //load file ram system.out.println("czas ladowania pliku: "+ (stoptime=system.currenttimemillis()-starttime)); starttime=system.currenttimemillis(); hm=new hashmap<string, integer>(); makelist(); system.out.println("czas tworzenia listy wyrazow: "+ (stoptime=system.currenttimemillis()-starttime)); starttime=system.currenttimemillis(); printing(); system.out.println("czas drukowania listy wyrazow: "+ (stoptime=system.currenttimemillis()-starttime)); exit(); system.exit(0); } void loadfile(string filename){ try{ f=new file(filename+".txt"); sc=new scanner(f); } catch(exception e){ e.printstacktrace(); } templine=new string(sc.tostring()); btab=templine.getbytes(); baos=new bytearrayoutputstream(); try{ baos.write(btab); } catch(exception e){ e.printstacktrace(); } } void makelist(){ st=new stringtokenizer(baos.tostring()); integer ti; //temporary int for(string nt=st.nexttoken(); st.hasmoretokens()==true;nt=st.nexttoken()){ if(hm.containskey(nt)==false){ hm.put(nt, 1); } else{ hm.put(nt,(integer)hm.get(nt)+1); } } } void printing(){ string sl="slowo"; string lp="liczba powtorzen:"; string sl1=""; string lp1=""; for(string str:hm.keyset()){ sl1=sl+str; lp1=lp+hm.get(str).tostring(); system.out.printf("\n %-20s, %-20s",sl1,lp1); } } int exit() throws ioexception{ baos.close(); sc.close(); return 1; } public static void main(string []args){ try { javaram a=new javaram("jpjmmwkm"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
}
}
thanks in advance ;)
templine=new string(sc.tostring());
seems wrong; wants sc.nextline()
.
anyway, code confusing. code in constructor should go method, instance members, etc, etc.
Comments
Post a Comment