gpt4 book ai didi

Java 作用域问题

转载 作者:行者123 更新时间:2023-12-01 12:26:51 25 4
gpt4 key购买 nike

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

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 中的其他类方法时,问题就出现了。

最佳答案

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

Command c = new Command();

编译失败,出现错误

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

使命令静态将解决这个问题。

那么,

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

编译失败,出现错误

error: 'void' type not allowed here

这是 returnActions 定义的逻辑结果

public void returnActions1(String s1) {

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

public String returnActions1(String s1) {

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

关于Java 作用域问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com