实验一简单的单词识别程序一、实验内容设计一个识别单词的程序,从文本文件中读取字符串,将字符串中的字符根据空格逐个识别为单词,并比较单词是否为zero,one,two,three,four,five,six,seven,eight,nine,若是则输出该单词对应的阿拉伯数字。要求用JAVA语言、图形界面方式编程。(可参考实验指导书P110至P113)举例:若文本文件中的字符串为oneatarethreesix234eight则输出为1368二、程序代码DistinguishWord.javapackagezlf;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassDistinguishWordextendsJApplet{privateJFileChooserjfc=newJFileChooser(newFile(.));privateJTextAreajta1=newJTextArea();privateJTextAreajta2=newJTextArea();privateJButtonjlb1=newJButton(字符串的内容为:);privateJButtonjlb2=newJButton(其中英文对应的阿拉伯数字为:);privateJLabelfileName=newJLabel(Filename);privateJTextFieldjtf=newJTextField(30);privateJButtonbrower=newJButton(Brower);publicDistinguishWord(){JPanelp1=newJPanel();p1.setLayout(newBorderLayout());p1.setBorder(BorderFactory.createEmptyBorder(5,5,5,10));p1.add(jlb1,BorderLayout.NORTH);p1.add(newJScrollPane(jta1),BorderLayout.CENTER);add(p1,BorderLayout.CENTER);JPanelp2=newJPanel();p2.setLayout(newBorderLayout());p2.setBorder(BorderFactory.createEmptyBorder(5,5,5,10));p2.add(jlb2,BorderLayout.NORTH);p2.add(newJScrollPane(jta2),BorderLayout.CENTER);add(p2,BorderLayout.EAST);JPanelp3=newJPanel();p3.setLayout(newBorderLayout());p3.add(fileName,BorderLayout.WEST);p3.add(jtf);p3.add(brower,BorderLayout.EAST);add(p3,BorderLayout.SOUTH);jtf.setText(C:/1.txt);brower.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){open();}});jtf.addKeyListener(newKeyAdapter(){publicvoidkeyPressed(KeyEvente){if(e.getKeyChar()==KeyEvent.VK_ENTER)//按回车键执行相应操作;{Stringfile=jtf.getText();try{BufferedInputStreamin=newBufferedInputStream(newFileInputStream(file));byte[]b=newbyte[in.available()];in.read(b,0,b.length);jta1.append(newString(b,0,b.length));in.close();}catch(IOExceptione1){jta1.setText(Erroropening);}}}});jlb2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Strings=jta1.getText();jta2.setText(translateFigure(s));}});}privateStringtranslateFigure(Stringstring){String[]english={zero,one,two,three,four,five,six,seven,eight,nine};Strings=string;Stringstr=;Stringt=;intn=0;char[]ch=s.toCharArray();for(inti=0;is.length();i++){if(ch[i]==''||ch[i]=='.'||ch[i]==','){t=s.substring(n,i);n=i+1;for(intj=0;j10;j++){if(t.equals(english[j]))str=str+j;}}}returnstr;}privatevoidopen(){if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)open(jfc.getSelectedFile());}privatevoidopen(Filefile){try{BufferedInputStreamin=newBufferedInputStream(newFileInputStream(file));byte[]b=newbyte[in.available()];in.read(b,0,b.length);jta1.append(newString(b,0,b.length));in.close();jtf.setText(file.getName());}catch(IOExceptionex){jtf.setText(Erroropening+file.getName());}}}1.txtoneatarethreesix234eight三、实验结果截图:简单说明:字符串内容框内可输入内容,点击“其中英文对应的阿拉伯数字为:”按钮可得出结果,或者在Filename后输入文件路径与文件名后回车,字符串内容框会显示文本文件内容,此时再点击“其中英文对应的阿拉伯数字为:”按钮可得出结果,或者在“Brower”按钮查找文件显示在字符串内容框内,再点击“其中英文对应的阿拉伯数字为:”按钮可得出结果。(PS:1.输入的字符串最后需要加个句号/逗号或者空个格,否则最后一个单词无法识别。2.1.txt存放在C盘,默认情况下Filename显示“C:/1.txt”)