CamelCaser function in Java, problems with debugging -


i creating camelcase function in java takes string this_is_a_sentence , returns thisisasentence main problem comes handeling different characters in string.

so far have:

  import java.util.scanner;       public class main{       public static string camelcaser(string str){         char[] strchr = str.tochararray();          strchr[0] = strchr[0].touppercase;          for(int = 0; < strchr.length; i++){            if (strchr[i] == '_'){               strchr[i] = strchr[i+1].touppercase;               strchr[i+1] = "";            }         }         string newstr = new string(strchr);         return newstr;      }       public static void main(string[] args){         scanner input = new scanner(system.in);         system.out.println("string: ");         string str = input.next();         system.out.printf("converting: %s %s", str, camelcaser(str));      }   } 

my main problems seems can not alter individual characters same way used in c. have read can use class called character cant figure out how use it. java documentation on matter https://docs.oracle.com/javase/7/docs/api/java/lang/character.html did not me either.

i changed around bit of logic make work. easiest thing make work search string _, , capitalize following letter. after that, take resulting string , remove _'s. ended this:

import java.util.scanner;     public class caser{      public static string camelcaser(string str){        char[] strchr = str.tochararray();        strchr[0] = character.touppercase(strchr[0]);        for(int = 0; < strchr.length; i++) if (strchr[i] == '_') strchr[i+1] = character.touppercase(strchr[i+1]);        string reply = new string(strchr);        reply = reply.replace("_", "");        return reply;     }      public static void main(string[] args){        scanner input = new scanner(system.in);        system.out.println("string: ");        string str = input.next();        system.out.printf("converting: %s %s", str, camelcaser(str));     } } 

edit:

what string reply = new string(strchr); do?

this creates new variable named reply. variable string. new string(strchr) helper string class has whereby can give char[] , automatically turn string.

what reply = reply.replace("_", ""); do?

.replace(string, string) string method search said string (in case, reply) , search instances of _ , when finds them replace empty string (in case, blank "").


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -