0

I am having a hard time figuring out why my code will not work. I am trying to stop the output on a specific letter, but it keeps iterating through the entire string instead. This is what I have,

public static char stringIterator(String string) {      
    System.out.println(string);//Keep this line
    char lastChar = string.charAt(string.length() - 1);
    if (lastChar == 'M') {
        return lastChar;
    }
    else if (string.length() == 1) {
        return lastChar;
    }
    else {
        return stringIterator(string.substring(0, string.length() - 2));
    }
}
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
  • 1
    `return stringIterator(string.substring(0, string.length() - 2));` should be `return stringIterator(string.substring(0, string.length() - 1));` no? – GBlodgett Mar 15 '19 at 00:11
  • 3
    Possible duplicate of [How can I check if a single character appears in a string?](https://stackoverflow.com/questions/506105/how-can-i-check-if-a-single-character-appears-in-a-string) – GBlodgett Mar 15 '19 at 00:14
  • @Zephyr You are correct, but this is _actually_ an iterative function that calls upon itself. Similar concept though. – Jodast Mar 15 '19 at 00:14
  • What is your end goal here? To print out a new line, subtracting a single letter each time, until you reach an "M?" – Zephyr Mar 15 '19 at 00:18
  • @Zephyr thats exactly what im trying to do. i am trying to subtract the last character of the string and print out a new string until i find the character i am looking for. i hope that makes sense – Donald Martin Mar 15 '19 at 00:18
  • @Zephyr also i am trying to find this character recursively – Donald Martin Mar 15 '19 at 00:19
  • Then really all you need to do is change the last `return` statement as @GBlodgett said. – Zephyr Mar 15 '19 at 00:19
  • @Zephyr it should be changed to return stringIterator(string.substring(0, string.length() - 1)) ? – Donald Martin Mar 15 '19 at 00:20
  • Thanks everyone for the help. I got it to work now :) – Donald Martin Mar 15 '19 at 00:23

3 Answers3

2

if you want to just see if it has it then you would use

string.contains('char');

if you want to traverse/iterate then

for( int i = 0; i < string.length(); i++)
    if(string.at(i) == '#')
    { //whatever you want here
    }
0

You might be over-thinking this... Java has very good resources for dealing with Strings, check out the docs: Java String Documentation

if (string.contains('m')){
    //dostuff
    }

    if (string.endsWith('m')){
    //dostuff
    }

etc.

As for your iteration problem, you'll have to post the rest of your code, as it looks like your Loop is Calling stringIterator(String) from somewhere outside this method. (It's not really a stringIterator if it doesn't iterate anything)

Also this last line of code:

return stringIterator(string.substring(0, string.length() - 2));

Is recursive (calls itself)... which can cause you trouble. There's certainly uses for recursion, finding something in a 1d array is not one of those uses. Assuming your loop is working properly this could be what's preventing you from stopping the output.

Batman
  • 97
  • 9
0

public String substring(int begIndex, int endIndex) - returns a new string that is a substring of the string Parameters :
beginIndex : the begin index, inclusive.
endIndex : the end index, exclusive.
(eg): String string = "NAME"
string.substring(0, string.length() - 2) - returns "NA"
string.substring(0, string.length() - 1) - returns "NAM" . Use this, thus you will be able to subtract the last character of the string.

Iterative approach

     public static char stringIterator(String string) {
     char lastChar = string.charAt(0);
       for(int i = string.length()-1 ;i>=0;i--) {
           System.out.println(string);//Keep this line
           lastChar = string.charAt(i);
           if (string.length() == 1 || lastChar == 'M') {
              break;
           } else {
              string = string.substring(0, i);
           }
        }
        return lastChar;
      }
Harishma A
  • 61
  • 6