4

なぜ私がこれにひどく苦労しているのかわかりませんが、どんな助けでも大歓迎です.

コマンド、区切り文字、および値のリストを含むファイルを取り込む独自のトークナイザーを作成しています。次に、各「トークン」とそのタイプを出力します。

入力:AND 3, 4, 5 ; some comments

出力する必要があります:

AND --- command
3 --- value
, --- delimiter
4 --- value
, --- delimiter
5 --- value

私は今、私が出力している場所に取り組んでいます:

AND 3, 4, 5 --- delimiter

しかし、私はそれをさらに分解する必要があります。

私が現在いる場所は次のとおりです。

ArrayList<Token> tokenize(String[] input) {
    ArrayList<Token> tokens = new ArrayList<Token>();
    for (String str : input) {
        Token token = new Token(str.trim());
        //Check if int
        try{
            Integer.parseInt(str);
            token.type = "number";
        } catch(NumberFormatException e) {

        }
        if (token.type == null) {
            if (commands.contains(str))
                token.type = "command";
             else if (str.contains(",")) {
                token.type = "delimiter";
            } else if (destValues.contains(str))
                token.type = "destination";
            else
                token.type = "unknown";
        }

        if(! token.type.equals("unknown"))
            tokens.add(token);
    }
    return tokens;
}

この割り当てに関する実際の制約は、StringTokenizer と正規表現を使用できないことだけです。

4

2 に答える 2

2

入力が間違っているようです。これを試して入力を分割し、トークン化メソッドを使用してください。

import java.util.*;

public class Foo {

    public static void main( String[] args ) {


        String input = "AND 3,    4, 5   ; some comments";
        List<String> parts = new ArrayList<String>();

        // removing comments
        input = input.split( ";" )[0];

        // splits using spaces
        String[] firstPass = input.trim().split( " " );

        for ( String s : firstPass ) {

            // the current part cannot be empty
            if ( !s.trim().isEmpty() ) {

                // splits using comma
                String[] secondPass = s.split( "," );

                for ( String ss : secondPass ) {
                    parts.add( ss.replace( ",", "" ) );
                }

                // verifies if the current part has a comma
                // and if so, inserts it as a part
                if ( s.contains( "," ) ) {
                    parts.add( "," );
                }

            }

        }

        for ( String a : parts ) {
            System.out.println( a );
        }

    }

}

編集:私の最初の答えがうまくいったので、ここにいくつかのリファクタリングを含む完全な例があります...

import java.util.*;

public class MyTinyParser {

    private static final String COMMANDS = "AND OR FOO BAR";

    private List<String> extract( String input ) {

        List<String> parts = new ArrayList<String>();

        // removing comments
        input = input.split( ";" )[0];

        // splits using spaces
        String[] firstPass = input.trim().split( " " );

        for ( String s : firstPass ) {

            // the current part cannot be empty
            if ( !s.trim().isEmpty() ) {

                // splits using comma
                String[] secondPass = s.split( "," );

                for ( String ss : secondPass ) {
                    parts.add( ss.replace( ",", "" ) );
                }

                // verifies if the current part has a comma
                // and if so, inserts it as a part
                if ( s.contains( "," ) ) {
                    parts.add( "," );
                }

            }

        }

        return parts;

    }

    public List<Token> tokenize( String input ) {

        List<Token> tokens = new ArrayList<Token>();

        for ( String str : extract( input ) ) {

            Token token = new Token( str );

            // check if int
            try{
                Integer.parseInt( str );
                token.type = "number";
            } catch(NumberFormatException e) {
            }

            if ( token.type == null ) {

                if ( COMMANDS.contains(str)){
                    token.type = "command";
                } else if (str.contains(",")) {
                    token.type = "delimiter";
                } else {
                    token.type = "unknown";
                }

            }

            if( !token.type.equals( "unknown" ) ) {
                tokens.add( token );
            }

        }

        return tokens;

    }

    private class Token {

        String value;
        String type;

        Token( String value ) {
            this.value = value;
        }

        @Override
        public String toString() {
            return String.format( "Token[%s, %s]", value, type );
        }

    }

    public static void main( String[] args ) {

        MyTinyParser mtp = new MyTinyParser();
        List<Token> tokens = mtp.tokenize( "AND 3,    4, 5   ; some comments" );

        for ( Token t : tokens ) {
            System.out.println( t );
        }

    }

}
于 2012-10-25T21:26:27.027 に答える
2

Google の API の使用が許可されている場合は、以下のようなものを試すこともできます。

import com.google.common.base.Splitter;

public class Tmp {

    public static void main(String[] args) {
        String str = "AND 3, 4, 5 ; some comments";

        Iterable<String> stringIterable = Splitter.on(' ').trimResults()
                .omitEmptyStrings()
                .split(str);

        for (String str1 : stringIterable) {
            int commaIndex = str1.indexOf(",");
            if (commaIndex > 0) {
                System.out.println(str1.subSequence(0, commaIndex));
                System.out.println(",");
            } else {
                System.out.println(str1);
            }
        }


    }

}

印刷します

AND
3
,
4
,
5
;
some
comments

PS最高のコードではありません。さらに改善される可能性がありますので、お気軽に声をかけてください。

于 2012-10-25T21:37:57.513 に答える