#Recursive method for capitalizing string

1 messages · Page 1 of 1 (latest)

rustic beacon
#

why is this returning -1? And when I try to capitalize the first letter and not get the numerical value of what I am trying to convert, my output is blank

    char firstLetter = concept.charAt(0);
    int equi;
    String restOf = concept.substring(1);
    if(Character.isUpperCase(firstLetter) == false) {
        firstLetter = (char)((firstLetter + 32));
    }
    equi = Character.getNumericValue(firstLetter);
    return equi;
}```
upper thicketBOT
#

<@&987246399047479336> please have a look, thanks.

upper thicketBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

rustic beacon
#

that is to say, this returns blank output ```public static char Recursion(String concept) {
char firstLetter = concept.charAt(0);
String restOf = concept.substring(1);
if(Character.isUpperCase(firstLetter) == false) {
firstLetter = (char)((firstLetter + 32));
}

return firstLetter;

}```

upper thicketBOT
worn dew
#

If you're supplying a string with first letter small, then adding 32 would not give you capital letter. Hence you're getting -1 as output. Try
firstLetter = (char ) (firstLetter - 32) to convert small to caps.

rustic beacon
#

I'm trying to figure out how to make this recursive without having two separate methods. My base case is whether or not the first letter of concept is uppercase. I'm having trouble building the recursive case

#

I keep getting an out of bounds error for this

#
    char letter;
    char first = concept.charAt(0);
    int equi;
    
    if(Character.isUpperCase(concept.charAt(0)) == false) {
          first = (char)(first - 32);
    }
    letter = concept.charAt(position - 1);
    equi = (int) letter;
    if(equi >= 97 && equi <= 122) {
        letter = (char)(equi - 32);
    }
    
    Recursion(concept, position - 1);
    
    return concept;
}```
upper thicketBOT
# rustic beacon ```public static String Recursion(String concept, int position) { char lette...

Detected code, here are some useful tools:

Formatted code
public static String Recursion(String concept, int position) {
  char letter;
  char first = concept.charAt(0);
  int equi;
  if (Character.isUpperCase(concept.charAt(0)) == false) {
    first = (char ) (first - 32);
  }
  letter = concept.charAt(position - 1);
  equi = (int ) letter;
  if (equi >= 97 && equi <= 122) {
    letter = (char ) (equi - 32);
  }
  Recursion(concept, position - 1);
  return concept;
}
worn dew
#

The issues here are

  1. the concept string is not changed in each recursion. So it doesn't matter how many times you call the function, the string is not changed. you may have to pass the changed string in order to get the modified result.
  2. I'm not sure what is the initial position value. If it was 0, then position-1 will go out of bounds.

If you really want to use recursion, this is one way,

        char letter = concept.charAt(position);
        if (!Character.isUpperCase(letter)) {
            letter = (char ) (letter - 32);
        }
        concept = concept.substring(0, position)+ letter + concept.substring(position+1);
        if (position+1 < concept.length()) return Recursion(concept, position + 1);
        else return concept;
    }```
And you can call the function as
`Recursion(concept,0)`
upper thicketBOT
# worn dew The issues here are 1) the concept string is not changed in each recursion. So ...

Detected code, here are some useful tools:

Formatted code
public static String Recursion(String concept, int position) {
  char letter = concept.charAt(position);
  if (!Character.isUpperCase(letter)) {
    letter = (char ) (letter - 32);
  }
  concept = concept.substring(0, position) + letter + concept.substring(position + 1);
  if (position + 1 < concept.length()) return Recursion(concept, position + 1);
  else return concept;
}
upper thicketBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

rustic beacon
#

i'm facing the following error:

java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown```
the file exists and the directory path is definitely correct because I tried this in another IDE, in C, and was able to successfuly access the file. However I have tried to access it in two separate java ide and keep getting this error no matter what
worn dew
#

Yes, because you're using a file opening function that could throw this exception. You should anticipate this condition and add try catch or throw operation to your function.

rustic beacon
#

i didn't realize catch/throw was required for file i/o to even compile

worn dew
#

add

try{
// your code
}catch (FileNotFoundException e){
}
upper thicketBOT