Java范围问题

时间:2014-10-08 18:12:46

标签: java regex

有人可以查看我的代码并尝试帮助我解决这个问题吗?这是一个解析用户命令行查询的正则表达式应用程序。

public class InterpretCommand {


    public static void main(String[] args){

        String a;

        Command c = new Command();

        java.util.Scanner reader = new Scanner(System.in);
        System.out.println("Enter command: ");
        a = reader.nextLine();

        //String to be scanned is the user input
        String line = a;
        //identifies the order command
        String pattern1 = "(?<=(-)([o]\\s)).*";
        //identifies the filter command
        String pattern2 = "(?<=(-)([f]\\s)).*";

        //create pattern objects
        Pattern r1 = Pattern.compile(pattern1);
        Pattern r2 = Pattern.compile(pattern2);

        //create matcher object
        Matcher m1 = r1.matcher(a);
        Matcher m2 = r2.matcher(a);

        //for Order match
        if (m1.find()){
            String s1 = m1.group(0);
            System.out.println(s1);  ** this works **
            System.out.println(c.returnActions1(s1)); **says print not applicable for arguments**

        }
        else if (m2.find()){
            String s2 = m2.group(0);
            System.out.println(s2); **this works**
            System.out.println(c.returnActions2(s2));  **says print not applicable for arguments**
        }
        else{
            System.out.println("No match for given input");
        }

    }

    public class Command {

            String a;

            public void returnActions1(String s1){

                String[] commands = s1.split(",");

                for(int i=0; i<commands.length; i++){

                    if(commands[i].equals("TITLE")){
                        //SELECT "TITLE" from <dataframe>
                }
                    else if(commands[i].equals("DATE")){
                        //SELECT "DATE" from <dataframe>
                    }
            }
        }

            public void returnActions2(String s2){

                String[] commands = s2.split(",");

                for(int i=0; i<commands.length; i++){

                    if(commands[i].equals("TITLE")){
                        //ORDER <dataframe> by "TITLE"
                }
                    else if(commands[i].equals("DATE")){
                        //ORDER <dataframe> by "DATE"
                    }
                }
            }
    }


}

关于我搞砸什么的任何想法?我确定我没有正确调整我的变量范围,或者可能只是实例化它们。当我将主变量传递给main中的其他类方法时会出现问题。

1 个答案:

答案 0 :(得分:1)

假设您确实希望Command成为内部类:

Command c = new Command();

无法编译错误

error: non-static variable this cannot be referenced from a static context

制作命令static将解决此问题。

然后,

System.out.println(c.returnActions1(s1));

无法编译错误

error: 'void' type not allowed here

这是returnActions定义为

的逻辑结果
public void returnActions1(String s1) {

由于我不知道您在该方法中实际想做什么,我只能建议更改其返回类型

public String returnActions1(String s1) {

并添加一个return语句,返回该方法应返回的内容。

相关问题