-3

Title. I'm new to this site sorry if it's bad question. I'm trying to make it so that my method returns -1 if there isn't an alphabetical letter in the string. I already set up a way to iterate through the string but now I need to know how to see if it returns true if it finds ANY alphabetical letter in the string and -1 if not.

BokChoy
  • 13
  • 1
  • 2
    Probably not using `indexOf` but a regular expression should work – Dexygen Oct 18 '18 at 16:37
  • try casting to int? – Pedro Rodrigues Oct 18 '18 at 16:40
  • @PedroRodrigues, cast what to an `int`? How would this approach help? – KevinO Oct 18 '18 at 16:41
  • 1
    Possible duplicate of [How to check whether a string contains at least one alphabet in java?](https://stackoverflow.com/questions/14278170/how-to-check-whether-a-string-contains-at-least-one-alphabet-in-java) – KevinO Oct 18 '18 at 16:42
  • Well, the duplicate I suggested depends upon whether your statement of returning "true" if it has a character, not if you want to return the specific index of the first alphabetical character. So please specify exactly what you are trying to accomplish. – KevinO Oct 18 '18 at 16:43
  • whatever that string is, if a cast to int works it ain't letter, its a number. – Pedro Rodrigues Oct 18 '18 at 16:46
  • @PedroRodrigues Casting strings to int is not possible in Java. – Mark Rotteveel Oct 18 '18 at 18:51

1 Answers1

3

To find the index of the first letter in a string, use a regular expression:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
static int indexOfLetter(String input) {
    String regex = "[a-zA-Z]";
    //         or: "\\p{Alpha}";  // same as [a-zA-Z]
    //         or: "\\p{L}";      // all unicode letters
    Matcher m = Pattern.compile(regex).matcher(input);
    if (m.find())
        return m.start();
    return -1;
}

Test

System.out.println(indexOfLetter("123456789")); // prints: -1
System.out.println(indexOfLetter("12345ABCD")); // prints: 5
System.out.println(indexOfLetter("@#$XYZ%^&")); // prints: 3

If you use the \p{L} regex, it'll find accented letters too.

static int indexOfLetter(String input) {
    Matcher m = Pattern.compile("\\p{L}").matcher(input);
    return (m.find() ? m.start() : -1);
}

Test

System.out.println(indexOfLetter("...ñ...")); // 3  'LATIN SMALL LETTER N WITH TILDE' (U+00F1)
System.out.println(indexOfLetter("..ß.."));   // 2  'LATIN SMALL LETTER SHARP S' (U+00DF)
System.out.println(indexOfLetter(".Δ."));     // 1  'GREEK CAPITAL LETTER DELTA' (U+0394)

UPDATE

If you're not allowed to use regex ("not allow me to use additional import statements"), a simple search loop calling Character.isLetter will do:

static int indexOfLetter(String input) {
    for (int i = 0; i < input.length(); i++)
        if (Character.isLetter(input.charAt(i)))
            return i;
    return -1;
}

That does support Unicode letters, but not surrogate pairs. Given the rarity of letters outside the Basic Multilingual Plane (BMP), that's probably not an issue. If it is, the following change will support those too:

static int indexOfLetter(String input) {
    for (int i = 0; i < input.length(); i = input.offsetByCodePoints(i, 1))
        if (Character.isLetter(input.codePointAt(i)))
            return i;
    return -1;
}
Andreas
  • 154,647
  • 11
  • 152
  • 247