Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java string.indexOf()返回不同的子字符串——可能吗?_Java_String_Arraylist - Fatal编程技术网

使用Java string.indexOf()返回不同的子字符串——可能吗?

使用Java string.indexOf()返回不同的子字符串——可能吗?,java,string,arraylist,Java,String,Arraylist,我正在尝试构建一个简单的解释器。基本上,我使用此方法从ArrayList中的字符串获取我的HashMap的键。HashMap中的字符串可以以8种不同的可能性(8个关键字)开头。目前,我正在使用string.indexOf(“某物”)查找关键字字符串,但当然,如果我有多个关键字,这一点都不灵活 ArrayList中的所有字符串都可以分解为COMMAND+(指令)。这些命令映射到HashMap及其类。所以基本上这是一个两步的情况:第一次,我需要从字符串中获取第一个单词/标记,然后字符串的其余部分最好

我正在尝试构建一个简单的解释器。基本上,我使用此方法从
ArrayList
中的字符串获取我的
HashMap
的键。
HashMap
中的字符串可以以8种不同的可能性(8个关键字)开头。目前,我正在使用
string.indexOf(“某物”)
查找关键字字符串,但当然,如果我有多个关键字,这一点都不灵活

ArrayList
中的所有字符串都可以分解为COMMAND+(指令)。这些命令映射到
HashMap
及其类。所以基本上这是一个两步的情况:第一次,我需要从字符串中获取第一个单词/标记,然后字符串的其余部分最好在适当的类中进一步拆分/标记

是否仍然存在
string.indexOf()
可以通过某种方式操作以返回多个子字符串的索引?还是我必须去别处寻找其他方法?请告知

代码如下所示:

public void parseCommands() {
    List<String> myString = new ArrayList<String>();
    myString.add(new String("# A TPL HELLO WORLD PROGRAM"));
    myString.add(new String("# xxx"));
    myString.add(new String("STRING myString"));
    //myString.add(new String("LET myString= \"HELLO WORLD\""));
    //myString.add(new String("PRINTLN myString"));
    myString.add(new String("PRINTLN HELLO WORLD"));
    myString.add(new String("END"));

    System.out.println();
    for (String listString : myString)//iterate across arraylist
    {
        if (listString.startsWith("#", 0))//ignore comments starting with #
        {
            continue;
        }

        int firstToken = listString.indexOf("END");
        String command = listString;


        Directive directive = commandHash.get(command);
        if (directive != null) {
            directive.execute(listString);
        } else {
            System.out.println("No mapped command given");
        }
    }
}
public void parseCommands(){
List myString=new ArrayList();
add(新字符串(“TPL HELLO WORLD程序”);
add(新字符串(“#xxx”);
add(新字符串(“字符串myString”);
//add(新字符串(“让myString=\'helloworld\”);
//add(新字符串(“PRINTLN myString”);
add(新字符串(“PRINTLN HELLO WORLD”);
添加(新字符串(“结束”);
System.out.println();
for(String-listString:myString)//遍历arraylist
{
if(listString.startsWith(“#”,0))//忽略以开头的注释#
{
继续;
}
int firstToken=listString.indexOf(“END”);
String命令=listString;
指令=commandHash.get(命令);
if(指令!=null){
指令.execute(listString);
}否则{
System.out.println(“未给出映射命令”);
}
}
}

简短的回答是“否”。您可能希望使用String.split()、StreamTokenizer或StringTokenizer。

看起来AL中的每个字符串可能只是一个命令,也可能只是命令的命令和输入

我想您可以在这里使用
split
方法:

String[] parts = listString.split(" ");
如果
parts
的大小是一个,这意味着它只是一个命令,否则
parts[0]
是一个命令,其余的则输入该命令

使用它进行查找:

Directive directive = commandHash.get(parts[0]);
然后,如果返回
指令
,则

  • 如果
    parts
    的长度为1,则只需执行
    指令。execute()
  • 否则,使用
    部分的其余部分形成输入,并执行
    指令。执行(输入)
  • 如果不是这样的话,也许我不明白你想说什么

    另外,你看,它有各种各样的方法,你可以在这里使用

    更新:

    public interface Directive {    
        void execute(String input);
    }
    
    public class EndDirective implements Directive {
        @Override
        public void execute(String input) {
            // input will be neglected here
            // just do whatever you supposed to do
        }    
    }
    
    public class PrintlnDirective implements Directive {
        @Override
        public void execute(String input) {
            // input will be used here        
            // you might want to check if the input is null here
            // and write the code accordingly
            System.out.println(input);
        }    
    }
    

    使用它,您可以执行指令.execute(null)当您没有任何输入时,因为您需要相应的
    指令
    忽略输入或使用它(如果他们在期望某些输入时收到null,也可能会处理null)。

    indexOf
    不能被操作以返回除已返回内容以外的任何内容。如果不知道您的输入字符串是什么样子,就无法提供帮助--一个简单的
    split()
    就足够了吗?您可能想了解一下Java的正则表达式功能。。。需要一点阅读、尝试、出错和努力,但肯定能完成任务。您似乎没有使用
    firstToken
    。您的解释不够清楚,无法反映需求。那么,HashMap中的“END”键是什么呢?地图上的钥匙也限制在8个以内吗?如果是,为什么不使用它们?另外,使用
    新字符串(“”
    @βɛʋʋʋʋʋʋɢ是一种不好的做法,谢谢——你做对了。AL中的字符串都可以分解为command+input或command(仅在END语句的情况下)。如果我使用
    split()
    方法获取一个数组,其中0和1处有两个部分(
    parts[0]
    parts[1]
    )--我的接口指令只有这个方法
    public void execute(String listString)
    ;是否需要为
    指令添加另一个方法。执行(输入)
    ?谢谢是添加(重载)
    指令。执行()
    ,它不接受任何参数,您就可以了。或者您也可以将现有的更改为处理空输入。@bɛƨǤʋʋɢ如果我想将现有的更改为处理空输入,我需要在方法中修改什么?如果这是一个noob问题,很抱歉,但我只是认真地编程并学习Java不到2个月——这么多东西对我来说都是新的。看看这个方法是什么样子会很好。但是首先使用if语句查看输入是否为null
    if(input==null){/*表示无输入*/},else{/*表示有输入*/}的命令
    。和
    directive.execute(null)
    @βɛƨǤʋʋɢ接口看起来像这样,其中只有一个方法<代码>公共接口指令//将关键字映射到接口{public void execute(String listString);}