当前位置:编程学习 > JAVA >>

正则表达式和文件输入的应用——初学问些小问题,来帮我看看

初学java,发现不了问题,大家帮我看看!
要求是这样的:
(1)读取一个由英文单词组成的文本文件,该文件中的英文单词以空格、逗号、回车符分隔
(2)单击“下一个单词”按钮,可以把读取的下一个单词读到文本框中。
(3)在文本框中输入单词的前几个字符,单击“查找”按钮,可以把所有符合要求的单词显示在文本区中。
我的相关代码:
public class Word{   
public StringBuffer getEnglishWord(File file){  
StringBuffer englishWord=new StringBuffer(); 
try{   
FileReader fr=new FileReader(file);  
BufferedReader br=new BufferedReader(fr);
String s=null;    
int i=0;    
while((s=br.readLine())!=null) 
{    
englishWord.append(" "+s+" ");   
}  
br.close();
fr.close();
}   
catch(Exception e) {}  
return englishWord;   
}  
}

public void actionPerformed(ActionEvent e) {
   // TODO 自动生成的方法存根
File file=new File("E:\\JAVA\\English.txt");
     if(e.getSource()==next)
    {       
     String regex="\\w+";
     p=Pattern.compile(regex,Pattern.MULTILINE);   
     m=p.matcher(word.getEnglishWord(file)); 
     System.out.print(word.getEnglishWord(file));
    
    boolean boo=m.find(); 
    if(boo)
    {    
         oneword=m.group();
         tf.setText(oneword); 
     }
    }
     if(e.getSource()==search)
    {
     String regex=tf.getText();
     p=Pattern.compile(regex,Pattern.MULTILINE);   
     m=p.matcher(word.getEnglishWord(file)); 
     System.out.print(word.getEnglishWord(file));
    
     boolean boo=m.find(); 
    if(boo)
    {           
       ta.setText(m.group(0));  
       ta.setCaretPosition(0);
     }else{
    JOptionPane.showMessageDialog(search,"                    没有找到!","error",
            JOptionPane.PLAIN_MESSAGE);      
     }
    
    }
} 文件输入 正则表达式 java --------------------编程问答-------------------- 运行的如何,是报错运行不了?还是正常运行但不能读出数据?还是正常运行也能读出数据然后希望别人改进?
请正确描述你遇到的问题 --------------------编程问答--------------------
public class EnglistDemo {
private ArrayList<String> arr;
private int num = 0;//记录arr的角标
private JFrame j  = new JFrame();
private JTextField field = new JTextField(15);
private JButton next = new JButton("下一个");
private JTextArea findText = new JTextArea(6,15);
private JButton find = new  JButton("查找");
private JPanel panel = new JPanel();
/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new EnglistDemo().init();
}
public void init() throws IOException
{
show();
next.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(num < arr.size())
{
field.setText(arr.get(num));
num++;
}
else
{
num=0;
}
}
});
find.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String head = findText.getText();
findText.setText("");
for(int x = 0; x < arr.size(); x++)
{
if(arr.get(x).startsWith(head))
{
findText.append(arr.get(x));
}
}
}
});
panel.add(field);
panel.add(next);
panel.add(findText);
panel.add(find);
j.setPreferredSize(new Dimension(300, 200));
j.add(panel);
j.pack();
j.setVisible(true);
}
public void show() throws IOException
{
FileReader read= new FileReader("c:\\englist.txt");
int len;
char[] ch = new char[1024];
StringBuffer sbuff = new StringBuffer();
while((len = read.read(ch))!=-1)
{
sbuff.append(new String(ch,0,len));
}
arr = new ArrayList<String>();
//先根据回车符分割
String[] str= sbuff.toString().split("\r\n");
for(int x = 0; x < str.length; x++)
{
String[] temp = str[x].split(" ");
for(int y = 0; y<temp.length; y++)
{
String[] temp1 = temp[y].split(",");
for(int i = 0; i < temp1.length; i++)
{
arr.add(temp1[i]);
}
}
}
}

}


这是我刚刚写的小Demo,不知道是不是你想要的结果
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,