java扫描器到字节[]?
我必须将txt文件复制到RAM(使用ByteArrayOutputStream),然后列出所有单词和重复次数。 但当我尝试打印列表时,它会打印我
"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]
我在这里找到了问题所在(扫描仪不是字符串对象),但找不到解决方案
你能帮我查一下密码吗? {
公共类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 to 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();
}
}
}
}
提前感谢;)
# 1 楼答案
这似乎是错误的;你可能想要
sc.nextLine()
不管怎样,你的代码真的很混乱。构造函数中的代码应该进入另一个方法,实例成员太多等等