这是我写的"关键字搜索"的程序,经过调试,总是输出"无此内容",即便是有想要搜索的关键字,也是输出"无此内容",我个人认为是在GetText()方法上出了问题(不排除别的地方也有问题).请高人指点,可以从新定义GetText()方法,或者使用JAVA自己带有的方法也可以.本人是JAVA初学者,请多指教,谢谢
以下是我写的程序:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.String.*;
public class MyFrame extends Frame implements ActionListener{
 private TextField textfield;
 private Button btn;
 private Label Lbl;
 private TextArea textarea;
 public  String Keyword="你是在忙碌";
 public void OtherFrame(){
     setLayout(new FlowLayout());
     setSize(500,500);
     add(new Label("请输入关键字:"));
     add(textfield=new TextField(""));
     textfield.addActionListener(this);
       //响应文本区域的内容
     add(btn=new Button("确定"));
     btn.addActionListener(this);//响应点击操作
     add(textarea=new TextArea());
     textarea.setSize(300,300);
     textarea.setEditable(true);
     addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent e){
        System.exit(0);
 }
 });//窗口的关闭操作,这个是匿名类。
}
            /*************利用IO流将本地文件读取到TextArea中************/
public   String readFileByLines(String fileName) throws FileNotFoundException {
         File file = new File(fileName);      
         BufferedReader reader = new BufferedReader(new FileReader(file));//利用BufferedRreader流读取文件,其中嵌套FileReader方法
             try {
                 String tempString = null;//定义一个tempString临时变量
                 String tempString2 = "";//定义一个tempString2临时变量
                    int line = 1; //一次读入一行,知道读入null为文件结束
                    while((tempString = reader.readLine()) != null){//BufferedReader的一个特有的方法,就是Readline()方法,可以一行一行的读
                 tempString2=tempString2+tempString;//将所读取的内容连接起来,“+”不仅起加运算的作用,还起连接字符串的作用
                    line++;
              }
                    textarea.setText(tempString2);//setText()方法是在文本区设置文件的一个JAVA提供的方法,可以直接调用,并不用定义构造方法(此方法困惑我很久)。
                    reader.close();//此处的Reader流必须关闭,否则当程序大的时候系统将会崩溃
           
        } catch (IOException e){
             e.printStackTrace();
           
        }finally{
             if(reader != null) {
                 try {
                       reader.close();
                } catch(IOException e1) {
                       e1.printStackTrace();
                }
            }
            
        }
return fileName;
    }//异常处理还得加强练习,运用的还不是很熟练
/***************获取TextArea中的内容**************/
public   String GetText(){//获取TextArea中的内容
String text=null;
try {      //异常处理,可能读取不到这个.txt文件
text=readFileByLines("F:\\语录\\语录.txt");//同一个类中的方法之间可以直接调用,这里readFile()就是被直接调用   读取的是本地文件
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return text;//返回text
}
/**************比较字符串*************/
public  void  Compare() {  
int i=0;//循环变量 
        int sum=0;//计数器   
        String  FirstPartKeyword=Keyword.substring(0,1);//用substring()方法截取字符串   
String  PartOfText=GetText().substring(i,i+1);//同上   

        for(i=0;i<=GetText().length()-Keyword.length();i++){ //这里的循环次数是通过将文本长度与关键字长度进行比较,只需要比较到文本后数与关键字长度相等的字符串即可,否则将会出现异常
               if(FirstPartKeyword.equals(PartOfText)){   
                        String  str=GetText().substring(i,i+Keyword.length());  //截取同Keyword长度一样的字符串,以便进行比较 
                                if(str.equals(Keyword)){   
                                        sum=sum+1;//如果if 语句为真,则计数一次   
             }   
           }//第一个if 结束   
        }//for  结束   
        if(sum>0){   
            System.out.println("有此内容");   
            System.out.println("共出现"+sum+"次");   
        }
        else
        { 
            System.out.println("无此内容");   
        }   
  } 

/******************************************************************************/
public static void main(String[] args)throws IOException {
   MyFrame f=new MyFrame();
   f.setTitle("SearchWord");
   f.OtherFrame();
   f.readFileByLines("F:\\语录\\语录.txt");//本地文件,请自己改一下本地存在的文件
   f.show();
   f.Compare();
}
/************此监听器是用来显示用户想要查询的内容,并显示此字段出现几次,如果没有想要查询的程序,就提示用户没有此信息**************/
public void actionPerformed(ActionEvent e){

}//监听TextField中的操作
}

解决方案 »

  1.   

    粗略的看了一下觉得应该把
    String     PartOfText=GetText().substring(i,i+1);//同上
    一句放在循环里面并且把循环里的i都改成j
      

  2.   

    回------------javazh
    ........谢谢   不过要是能帮解决这个问题 ,我会更加感谢的~   !
      

  3.   

    回------------------lixkyx :
    我研究完了,我把GetText()方法删掉了,然后把获取text的方法定义在Compare()定义在中,用setText()方法就行(JAVA有这个方法)
    并把String     PartOfText=GetText().substring(i,i+1);放在for循环里面了
    这是代码:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.String.*;
    public class MyFrame extends Frame implements ActionListener{
     private TextField textfield;
     private Button btn;
     private Label Lbl;
     private TextArea textarea;//显示文本区域
     private TextArea DelicateArea;//显示搜索结果区域
     public  String Keyword="   ";
     public void OtherFrame(){
         setLayout(new FlowLayout());
         setSize(500,500);
         add(new Label("请输入关键字:"));
         add(textfield=new TextField(""));
         textfield.addActionListener(this);
           //响应文本区域的内容
         add(btn=new Button("确定"));
         btn.addActionListener(this);//响应点击操作
         add(textarea=new TextArea());
         textarea.setSize(300,300);
         textarea.setEditable(true);
         add(DelicateArea=new TextArea());
         DelicateArea.setSize(300,300);
         DelicateArea.setEditable(true);
         addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
            System.exit(0);
     }
     });//窗口的关闭操作,这个是匿名类。
    }
                /*************利用IO流将本地文件读取到TextArea中************/
    public   String readFileByLines(String fileName) throws FileNotFoundException {
             File file = new File(fileName);      
             BufferedReader reader = new BufferedReader(new FileReader(file));//利用BufferedRreader流读取文件,其中嵌套FileReader方法
                 try {
                     String tempString = null;//定义一个tempString临时变量
                     String tempString2 = "";//定义一个tempString2临时变量
                        int line = 1; //一次读入一行,知道读入null为文件结束
                        while((tempString = reader.readLine()) != null){//BufferedReader的一个特有的方法,就是Readline()方法,可以一行一行的读
                     tempString2=tempString2+tempString;//将所读取的内容连接起来,“+”不仅起加运算的作用,还起连接字符串的作用
                        line++;
                  }
                        textarea.setText(tempString2);//setText()方法是在文本区设置文件的一个JAVA提供的方法,可以直接调用,并不用定义构造方法(此方法困惑我很久)。
                        reader.close();//此处的Reader流必须关闭,否则当程序大的时候系统将会崩溃
               
            } catch (IOException e){
                 e.printStackTrace();
               
            }finally{
                 if(reader != null) {
                     try {
                           reader.close();
                    } catch(IOException e1) {
                           e1.printStackTrace();
                    }
                }
                
            }
    return fileName;
        }//异常处理还得加强练习,运用的还不是很熟练 /**************比较字符串*************/
    public  void  Compare() {  
    int i=0;//循环变量 
            int sum=0;//计数器 
            String  text=textarea.getText();//获取TextArea中的内容
            String  FirstPartKeyword=Keyword.substring(0,1);//用substring()方法截取字符串   
            
            for(i=0;i<=text.length()-Keyword.length();i++){ //这里的循环次数是通过将文本长度与关键字长度进行比较,只需要比较到文本后数与关键字长度相等的字符串即可,否则将会出现异常
                    String  PartOfText=text.substring(i,i+1);//同上
                         if(FirstPartKeyword.equals(PartOfText)){   
                                  String  str=text.substring(i,i+Keyword.length());  //截取同Keyword长度一样的字符串,以便进行比较 
                                        if(str.equals(Keyword)){   
                                                sum=sum+1;//如果if 语句为真,则计数一次   
                 }   
               }//第一个if 结束   
            }//for  结束   
                           if(sum>0){   
                             DelicateArea.setText("有此内容"+"    共出现"+sum+"次");//将搜索所得到的数据设置显示在DelicateArea中     
            }
                           else
            { 
                             DelicateArea.setText("无此内容");// 功能同上  
            }   
      } 

    /******************************************************************************/
    public static void main(String[] args)throws IOException {
       MyFrame f=new MyFrame();
       f.setTitle("SearchWord");
       f.OtherFrame();
       f.readFileByLines("F:\\语录\\语录.txt");//本地文件
       
       f.show();
       f.Compare();
    }
    /************此监听器是用来显示用户想要查询的内容,并显示此字段出现几次,如果没有想要查询的程序,就提示用户没有此信息**************/
    public void actionPerformed(ActionEvent e){

    }//监听TextField中的操作
    }
      

  4.   

    回------------lixkyx 谢谢 支持!!!