user3053252
user3053252

Reputation: 31

Alternative to this code in Java

So I have made some code for my AP Computer Science class but my teacher is asking me to not use char or token in my code. I have this one code in particular that I need an alternative (non char) version for.

// returns the first nonzero digit of a string, 0 if no such digit found
    public static int firstDigitOf(String token) {
        for (char ch : token.toCharArray()) {
            if (ch >= '1' && ch <= '9') {
                return ch - '0';
            }
        }
        return 0;
    }

So yes please help me. This isn't homework its a part of a LARGE project so entire lines of code would be appreciated in particular.

or (char ch : token.toCharArray()) {

This is what I have the most trouble with, I just dont know another way to write this.

Upvotes: 0

Views: 252

Answers (4)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

you can use this

String token = "helo100s23h04dsd sdksjdksa";
token = token.replaceAll("[^1-9]", "");
 // in this case token value will be -> 1234, and the first none zero digit is 1
 if (token.length() <= 0) {
  // if there is no numbers in token larger than 0
    return 0;
   } else {
   return Character.getNumericValue(token.charAt(0));
 }

Upvotes: 2

dansalmo
dansalmo

Reputation: 11704

This will work if the input string is all digits:

public static int firstDigitOf(String digits) {
    if (digits.length() == 0)
        return 0;
    int firstDigit = Integer.parseInt(digits.substring(0, 1));
    if (firstDigit > 0)
        return firstDigit;
    return  firstDigitOf(digits.substring(1));
}

Iterative version:

public static int firstDigitOf(String digits) {
    int firstDigit = 0;
    while (digits.length() != 0) {
        firstDigit = Integer.parseInt(digits.substring(0, 1));
        if (firstDigit > 0)
            break;
        digits = digits.substring(1);
    }
    return  firstDigit;
}

If the string might have non-digits, you need to do this:

public static int firstDigitOf(String token) {
    if (token.length() == 0)
        return 0;
    try {
        int firstDigit = Integer.parseInt(token.substring(0, 1));
        if (firstDigit > 0 && firstDigit < 10)
            return firstDigit;
    } catch (NumberFormatException e) {
    }
    return  firstDigitOf(token.substring(1));
}

Upvotes: 1

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

In the spirit of overkill, here's a regular expression version. Note that there is no use direct use of char - it is all done with String.

  private static Pattern nonZeroDigit = Pattern.compile("[123456789]");

  public static int firstDigitOf(String token) {
    Matcher digitMatcher = nonZeroDigit.matcher(token);
    if (digitMatcher.find()) {
      return Integer.parseInt(digitMatcher.group());
    } else {
      return 0;
    }
  }

Upvotes: 0

Thomas Junk
Thomas Junk

Reputation: 5676

I came up with the following:

public Integer firstDigitOf(String s) {
    s=s.replaceAll("[^1-9]","");
    return (s.isEmpty())?0:Integer.parseInt(s.substring(0,1));
}

Just replace every non-numerical content and give either 0 or first digit.

Upvotes: 0

Related Questions