Austin Green
Austin Green

Reputation: 37

how to compare a character to individual characters in a string?

I am trying to determine if the character at the beginning of a string is not equal to any of the characters in another string.

My problem is that even if the character is an 'a' for example it still trips on any of the other characters.

Here is the code I have so far:

    public void searcher(String s)
    {
    boolean done = false;
    String checker = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int x = 0; x < checker.length(); x++)
    {
        if(s.charAt(0) != checker.charAt(x))
        {
            System.out.println("Answer: " + s + "is not a valid identifier\n");
            break;
        }
    }

Upvotes: 0

Views: 1321

Answers (6)

definitely undefinable
definitely undefinable

Reputation: 883

You could also use a small regular expression matching method.. (code not tested ;)

also.. especially when new to coding, extract as much functionality as possible into neat small methods.

public void searcher(String s) {
  String checker = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

  if (!matches(s.substring(0,1), checker))
    System.out.println("Answer: " + s + "is not a valid identifier\n");
}

public boolean matches(String test, String pattern) {
  Pattern p = Pattern.compile("["+pattern+"]");
  Matcher m = p.matcher(test);
  return m.matches();
}

Quick link to more info about regular expressions in Java: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

Upvotes: 0

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91299

You need to use indexOf instead of charAt, like so:

if (checker.indexOf(s.charAt(0)) != -1) {
  // Valid identifier.
} else {
  // Invalid identifier.
}

In other words, check if your checker string contains (indexOf != -1) the first character of s (s.charAt(0)).

Upvotes: 3

Kuldeep Jain
Kuldeep Jain

Reputation: 8598

I think what you want to do is to print System.out.println("Answer: " + s + "is not a valid identifier\n"); when checker does not contain the first character of String s. If so use this code to do it:

    public void searcher(String s)
    {
        String checker = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        if (checker.indexOf(s.charAt(0)) == -1)
        {
            System.out.println("Answer: " + s + " is not a valid identifier\n");
        }
    }

Upvotes: 3

paulsm4
paulsm4

Reputation: 121599

A few minor changes might help clarify things:

 public void searcher(String s)
 {
    String testValues = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    boolean found = false;
    char target = s.charAt(0);
    for (int i = 0; i< testValues.length(); i++)
    {
        if(target == testValues.charAt(i))
        {
            found = true;
            break;
        }
    }
    if (found) 
      System.out.println ("target (" + target + "): FOUND");
    else
      System.out.println ("target (" + target + "): NOT FOUND");
 }

Equivalently, you can just lose the whole loop and use String.indexOf() instead :)

Upvotes: 1

Karim
Karim

Reputation: 5308

If you want to keep your code as it is and modify a little bit, this is the change you need to do :

public void searcher(String s)  {

boolean isValid = false;
String checker = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (int x = 0; x < checker.length(); x++)
{
    if(s.charAt(0) == checker.charAt(x))
    {
        isValid = true;
        break;
    }
}

if(isValid == true) //or if(isValid)
    System.out.println("Answer: " + s + "is a valid identifier\n");
else
    System.out.println("Answer: " + s + "is not a valid identifier\n");
}

Explanation: what I did is : By default suppose the first character of 's' is not valid, then iterate through the characters of the default string, if there is a match, then s is VALID and Break !

Hope this helps !

Upvotes: 1

aviraldg
aviraldg

Reputation: 9154

boolean isValidIdentifier = checker.indexOf(s.charAt(0)) < 0;

Upvotes: 8

Related Questions