SRK
SRK

Reputation: 1040

How to find specific character in Java

I have a string like a>5 and b<6 or c>=7 in java

When I check whether string contains > then output is true for both > and >=

How can I restrict my check only to specific character?

How can I use matches function?

Upvotes: 1

Views: 4186

Answers (5)

Voo
Voo

Reputation: 30206

I just threw this together based on the updated specifications. Basically a real simple parser rather mechanically created: (And I'm not good with naming at 7 in the morning oh well)

public class Main {

    public static void main(String[] args) {
        String test = "a > b >= c > x";
        Main m = new Main(test);
        System.out.println(m.getTokenNumber());

        test = "aasdfasdf asdfdasf";
        m = new Main(test);
        System.out.println(m.getTokenNumber());
    }

    private String input;
    private int pos;

    public Main(String input) {
        this.input = input;
        pos = 0;
    }

    public TokenNumber getTokenNumber() {
        TokenNumber tokenNumber = new TokenNumber();
        Token t = nextToken();
        while (t != Token.NONE) {
            tokenNumber.addToken(t);
            t = nextToken();
        }
        return tokenNumber;
    }

    private Token nextToken() {
        while (pos < input.length() && input.charAt(pos) != '>') pos++;
        if (pos == input.length()) return Token.NONE;
        pos++;
        if (pos == input.length() || input.charAt(pos) != '=') return Token.GREATER;
        return Token.GREATER_EQUAL;
    }

    enum Token {
        GREATER, GREATER_EQUAL, NONE;
    }

    static class TokenNumber {
        public int greater;
        public int greater_than;

        public void addToken(Token t) {
            if (t == Token.GREATER) greater++;
            else greater_than++;
            assert t != Token.NONE;
        }

        public String toString() {
            return String.format("Greater: %d%nGreater Than: %d", greater, greater_than);
        }
    }

}

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38511

You want to discriminate between greater than and greater than or equal to. Why not write a method that returns the operator?

enum Operator { 
...
}

public Operator getOperator(String s) { 

    if(s.contains(">=")) { 
       return Operator.GREATER_THAN_OR_EQUAL_TO;
    } else if (s.contains(">") {
       return Operator.GREATER_THAN;
    }

}

If the input can be a complex expression that contains multiple operators, instead of contains, try using indexOf(...) and look ahead one character for the '='.

Upvotes: 2

alf
alf

Reputation: 8513

Your mistake is, you think of a lexical entity, >=, as of a "character." That will bite you more than once, as there actually are two characters, > and =, and > is indeed here. So depending on what you need, the answer may be different.

Why don't you want to see >= found?

What usage of > is of interest for you? Will e.g. <tag>some text</tag> be a proper string which you'd prefer to allow?

Upvotes: 2

Stas Jaro
Stas Jaro

Reputation: 4885

"your string >".contains(">");// returns true

Upvotes: 0

kcdragon
kcdragon

Reputation: 1733

boolean value = Pattern.compile(">").matcher("a>5").find(); // returns true

Upvotes: -1

Related Questions