mercredi 24 février 2016

Final return statement, ignore

Im working on a token iterator (valid tokens, "true, false, "true", "&", "!", "(", "false", "^", "true", ")".

The code is working, my question is about return values. I often run into this problem, I have return statements, but the final return statement throws off my result by duplicating the last return statement.

I think I know for sure the error lays within my placement of { and } and while i've learned they aren't necessary, since there's so many nested if's i feel they are necessary.

This seems to be a common problem to me and others ive worked with, does anyone have an idea of how to prevent this problem from happening? Thanks!

My code outputs:

line: [ ! BAD (true ^ false) % truelybad] next token: [!]

next token: [(]

next token: [true]

next token: [^]

next token: [false]

next token: [)]

next token: [)]

and should output

next token: [!]

next token: [(]

next token: [true]

next token: [^]

next token: [false]

next token: [)]

public class TokenIter implements Iterator<String> {
ArrayList<String> token = new ArrayList<String>();
static int count = 0;
// input line to be tokenized
private String line;

// the next Token, null if no next Token
private String nextToken;

// implement
public TokenIter(String line) {
    this.line = line;

}

@Override
// implement
public boolean hasNext() {
    // System.out.println(count);
    return count < line.length();
}

@Override
// implement
public String next() {
    while (hasNext()) {

        char c = line.charAt(count);
        if (c == '!' || c == '!' || c == '^' || c == '(' || c == ')') {
            token.add(Character.toString(c));
            count++;
            nextToken = Character.toString(c);
            return nextToken;
                } else if (c == 't' || c == 'T') {
                    count++;
                    c = line.charAt(count);
                        if (c == 'r') {
                            count++;
                            c = line.charAt(count);
                            }
                                if (c == 'u') {
                                    count++;
                                    c = line.charAt(count);
                                }
                                    if (c == 'e') {
                                        count++;
                                        c = line.charAt(count);

                                    }if (c == ' ' || c == '!' || c == '!' || c == '^' || c == '(' || c == ')'){
                                        token.add("true");

                                        nextToken = "true";
                 //count++;
                                        return nextToken;
                                    }

        } else if (c == 'f' || c == 'F') {
            count++;
            c = line.charAt(count);
            if (c == 'a') {
                count++;
                c = line.charAt(count);
            }
            if (c == 'l') {
                count++;
                c = line.charAt(count);
            }
            if (c == 's') {
                count++;
                c = line.charAt(count);
            }
            if (c == 'e') {
                count++;
                c = line.charAt(count);

            }
            if (c == ' ' || c == '!' || c == '!' || c == '^' || c == '(' || c == ')'){
                token.add("false");

                nextToken = "false";
                // count++;
                return nextToken;
            }

        } else if (c == ' ') {
            count++;
        } else {
            count++;
        }

    }


    return nextToken;
}

@Override
// provided, do not change
public void remove() {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();
}

// provided
public static void main(String[] args) {
    String line;
    // you can play with other inputs on the command line
    if (args.length > 0)
        line = args[0];
    // or do the standard test
    else
        line = " ! BAD (true ^ false) % truelybad";
    System.out.println("line: [" + line + "]");
    TokenIter tokIt = new TokenIter(line);
    while (tokIt.hasNext())
        System.out.println("next token: [" + tokIt.next() + "]");
}

}

Aucun commentaire:

Enregistrer un commentaire