试图在textArea中查找特定单词

时间:2014-05-11 11:54:00

标签: java string swing character jtextarea

所以我在这里要做的是通过使用String code = ta.getText()方法从JTextArea获取文本。 ta位于TextIDE类中,声明为public static JTextArea ta = new JTextArea();

然而,之后我检索了用户输入的文本,我想把它变成一个char数组,所以我使用方法code.toCharrArray();之后在方法compileText中我使用for-statement来查找字母' p'如果找到了,我调用方法p(),然后发送计数器i和实际字符数组的位置,这样我就可以继续搜索字母' r'我这样做直到班级找到印刷字。每当它发生时,它应该附加另一个JTextArea,它就像一个控制台,位于一个名为Listener的类中,所以我使用Listener.c.jt1.append(s); s等于打印后的任何内容,直到找到分号为止,&#39 ;;'。

但它不起作用,我没有错误,所以我认为这里存在逻辑错误...... 谢谢你的帮助!! :)

public class Compiler { 

    String code = TextIDE.ta.getText(); //Gets the text that was inputted by the user

    public Compiler(){ //Constructor

        if(!(code.endsWith("programEnd"))){
            Listener.c.jt2.append("Error - Code must end with line: programEnd on the last line");
        }else{
            compileText(code, 0);
        }
    }

    public void compileText(String code, int start){ //Starts the compiling
        char[] codeArray =  code.toCharArray();
        System.out.println(codeArray.length);

        for(int i = start; i < codeArray.length; i++){ //Looks for character 'p'
            switch(codeArray[i]){
                case 'p':   p(codeArray, ++i);
                            System.out.println("p");
                    break;
            }
        }
    }

    public void p(char[] codeArray, int start){
        for(int i = start; i < codeArray.length; i++){
            switch(codeArray[i]){
                case 'r':   r(codeArray, ++i);
                            System.out.println("r");
                    break;
            }
        }
    }

    public void r(char[] codeArray, int start){
        for(int i = start; i < codeArray.length; i++){
            switch(codeArray[i]){
                case 'i':   i(codeArray, ++i);
                            System.out.println("i");
                    break;
            }
        }
    }

    public void i(char[] codeArray, int start){
        for(int i = start; i < codeArray.length; i++){
            switch(codeArray[i]){
                case 'n': n(codeArray, ++i);
                            System.out.println("n");
                    break;
            }
        }
    }

    public void n(char[] codeArray, int start){
        for(int i = start; i < codeArray.length; i++){
            switch(codeArray[i]){
                case 't': t(codeArray, ++i);
                            System.out.println("t");
                    break;
            }
        }
    }

    public void t(char[] codeArray, int start){
            String s = "";
            for(int i = start; i < codeArray.length; i++){
                if(!(codeArray[i] == ';')){
                    s += codeArray[i];
                }else{
                    Listener.c.jt1.append(s);
                    compileText(code, ++i);
                    break;
                }
            }
    }
}

1 个答案:

答案 0 :(得分:0)

  

我想在char数组中找到print这个词,我该怎么做?

为什么使用字符数组?您只需搜索文本字符串:

String text = textArea.getText();
System.out.println( text.indexof( "print" );
相关问题