细节之重——Java编程小感悟

    xiaoxiao2021-12-14  20

    最近在研究共词分析。看了不少算法,于是自习跃跃欲试,想用学了没多久的java进行实践。经过自认为周密的计划,我兴奋地开始实践,不想出师未捷,第一小步就遇到了找不出的错误。

    第一步,我想实现,读入一个文本,统计其中不同词的出现次数。这样的程序并不难,我一鼓作气写了出来:

    import java.io.*; import java.util.*; public class cotest { public static void main(String args[]){ File readFile=new File("F:/code/coword.txt"), writeFile= new File("F:/code/hello.txt"); try{ FileReader inOne=new FileReader(readFile); BufferedReader inTwo=new BufferedReader(inOne); String s=null; int n; LinkedList<str_nu> words=new LinkedList<str_nu>(); while((s=inTwo.readLine())!=null){ Scanner sc=new Scanner(s); while(sc.hasNext()){ String a=new String(); a=sc.next(); //System.out.println(a); n=getIndex(words,a); //System.out.println(n); if(n!=-1){ words.get(n).num++; System.out.println(words.get(n).num); } else{ str_nu new_word=new str_nu(); new_word.word=a; new_word.num=1; words.add(new_word); } } } for(int i=0;i<words.size();i++){ System.out.println(words.get(i).word+" "+words.get(i).num); } inOne.close(); inTwo.close(); } catch(IOException e){ System.out.println(e); } } private static int getIndex(LinkedList<str_nu> words, String a) { // TODO Auto-generated method stub for(int i=0;i<words.size();i++){ if(words.get(i).word==a){ System.out.println(i); return i; } } return -1; } } class str_nu{ String word=new String(); int num=0; }刚写出来的时候还挺得意,但实验结果却让我傻了眼:程序根本不能统计词语的出现次数,只能把词语一个个打出来。我百思不得其解,怎么调试都还是这样的结果。我高涨的热情也在一次次调试的过程中,消耗殆尽了。

    然后我就开始了花式询问,在小木虫、、百度知道上提问,还特意找了一个学软件的同学,让他帮忙调试。

    状况如下:

    小木虫:悬赏20,一人回复,不过他的java水平好像还不如我,没回答到点子上。‘

    百度知道:悬赏20,没人回答。

    同学:建议我用HashMap重新编写,并给出了他的程序。他是站在比我高很多的层次上,他写的代码,我只能勉强看懂。运行结果其实也不是我想要的。他的解释是,我的代码有问题,每次num都会被重置为0。但是我又仔细检查好几遍之后不敢苟同。

    :没有悬赏(因为没钱),一人回复,终于解决了我的问题。

    字符串的精确比较不能用==,要用.equals()!

    字符串的精确比较不能用==,要用.equals()!

    字符串的精确比较不能用==,要用.equals()!

    然后代码变成了这样:

    import java.io.*; import java.util.*; public class coword { public static void main(String args[]){ File readFile=new File("F:/code/coword.txt"), writeFile= new File("F:/code/hello.txt"); try{ FileReader inOne=new FileReader(readFile); BufferedReader inTwo=new BufferedReader(inOne); String s=null; int n; LinkedList<str_nu> words=new LinkedList<str_nu>(); while((s=inTwo.readLine())!=null){ Scanner sc=new Scanner(s); while(sc.hasNext()){ String a=new String(); a=sc.next(); //System.out.println(a); n=getIndex(words,a); //System.out.println(n); if(n!=-1){ words.get(n).num++; } else{ str_nu new_word=new str_nu(); new_word.word=a; new_word.num=1; words.add(new_word); } } } for(int i=0;i<words.size();i++){ System.out.println(words.get(i).word+" "+words.get(i).num); } inOne.close(); inTwo.close(); } catch(IOException e){ System.out.println(e); } } private static int getIndex(LinkedList<str_nu> words, String a) { // TODO Auto-generated method stub for(int i=0;i<words.size();i++){ if(words.get(i).word.equals(a)){ return i; } } return -1; } } class str_nu{ String word=new String(); int num; } 然后结果就对了!!!

    从这件事中我有两点收获:

    第一,不要在学了一点点知识之后就盲目上手,知识是需要积累的,兔子只知道吃地上的草,而长颈鹿却能看到树上的叶子。

    第二,细节虽然不能决定你的成功,但往往决定你的失败。equals这个问题,其实之前我也见过,但当时认为自己肯定与java无缘,就没有用心去记。所以,不要太功利地区学习,要时刻保持对周围的好奇心。

    好了就是这样。我相信经常生产鸡汤能使人心情愉悦。快到周末了,希望自己能好好利用时间吧。

    转载请注明原文地址: https://ju.6miu.com/read-962338.html

    最新回复(0)