0

I'm trying to take a string as an input (around 5 - 7 characters), iterate through each character and check whether or not it matches a character from an array. For example:

static String Registration(String reg) {
    int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9,};
    if (reg.length() >= 7) {
        for (int i = 0; i < reg.length(); i++) {
            reg.indexOf(numbers[i]);
        }
    }
}

The above is clearly wrong. However, I hope it illustrates what I'm trying to do. For example, if the string was "H3llo", I would want to check each character in the string and see if it matches with any items in the array. If it does, I would like to store the index as an int.

This is probably not an efficient way of doing it, but it's the only way I can think of with my current level of learning.

5
  • 3
    What do you mean with "whether or not it matches a character from an array"? Your array is an array of integers and not characters. Are you trying to see if the String contains a number 0-9? Commented Dec 7, 2021 at 14:05
  • What exactly is the array of characters you want to check, as @OHGODSPIDERS asked, it makes a difference if you just wanna check the presence of integers in your string or any other certain characters. Commented Dec 7, 2021 at 14:15
  • Be aware that what you are trying to do is something that Regular Expressions has standardized Commented Dec 7, 2021 at 14:18
  • @ControlAltDel, I think @ItchyPrune just wants to use some basic iteration and comparison operators instead of regex. Commented Dec 7, 2021 at 14:22
  • 1
    @ShivangGupta I agree with you. However I am a RegEx "evangelist" - I want the world to learn about the "glory" of what text search can be! Not that I expect that everyone in practical life will use them - I just want people to be aware. Commented Dec 7, 2021 at 14:31

1 Answer 1

1

there are several problems with your code here:

  • your numbers array is of type int, however you want to compare the characters. use char[] instead and use chars in your array instead of ints like this: '0', '1', ...
  • indexOf is not a existing method for arrays, instead use a for loop to iterate through all the entries of the array and compare them by hand
  • your function claims to return a String public String Registration but you want to return an int. change this to public int Registration.
  • You have no return in your function. you have to use the keyword return in your function if your function should return something.

A possible solution may look like this:

public static int Registration(String reg) {
  char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7' , '8', '9',}; // use a char array char[] and add numbers as chars to array.
  if (reg.length() >= 7) {
    for (int i = 0; i < reg.length(); i++) {
      char character = reg.charAt(i);
      for(char number : numbers)
      {
        if(number == character)
        {
          return i; // found a matching character, return the index of the matching character
        }
      }
    }
  }
  // return -1 for the case that we find nothing.
  return -1;
}

please try to understand what happens here and do not just copy and paste the solution.

Note: the thing you wish to do can be easily implemented with RegEx, however, this may be too hard for you at your current stage.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you for the detailed response. The nested for each loop is exactly what i was looking for. Yes, i'm not up to Regex in my learning yet but will be sure to keep it in mind. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.