1

below is my code that checks the incoming model and modifies the source accordingly, checking if its ALLCAPS or Firstcap. The problem I am having is when the model contains a symbol e.g. matchCase("I'm","apple"). This would return apple, when it's supposed to return Apple. On the other hand, If I use "Im", it modifies it correctly to "Apple". Is there a way i can modify it that would work. I tried to run a few methods but I keep getting stuck

public static String matchCase(String model, String source){
    boolean o = true;
    if(model.toUpperCase().equals(model)){
      source = source.toUpperCase();
    }
    if(Character.isUpperCase(model.charAt(0))){
      for(int i=1;i<model.length();i++){
         if(Character.isLowerCase(model.charAt(i)) == false){
           o = false;
         }
      }
     // if(o == model.length()-1){
      if(o == true){
        String can = "";
        for(int j=0;j<source.length();j++){
          if(j==0){
            can += Character.toUpperCase(source.charAt(j)); }
          else{
            can += source.charAt(j);
          }
        }
        source = can;
//    Character.toUpperCase(source.charAt(0));
       
  }
    }
    
    return source;
  }
}

4
  • Can you please explain the logic in more clear way? What are you exactly trying to achieve? Commented Mar 2, 2017 at 18:22
  • This would return apple, when it's supposed to return Apple. On the other hand, If I use "Im", it modifies it correctly to "Apple". It all sounded good. What's the problem? Commented Mar 2, 2017 at 18:24
  • It takes in a String as a model and one is the source. Lets say the model is "CHECK" and the source is "hello". It will check the model and modify the source to HELLO as well since the model is all caps. Similarly if its model is "Check" and source is "hello", it will modify the source to "Hello". Commented Mar 2, 2017 at 18:26
  • What should source be converted to if model is cHecK and source is hELLO? Commented Mar 2, 2017 at 18:28

2 Answers 2

1

I think your problem comes from the fact that

Character.isLowerCase('\'') // is false

You should change this test

if(Character.isLowerCase(model.charAt(i)) == false)

By

if(Character.isUpperCase(model.charAt(i)))
Sign up to request clarification or add additional context in comments.

2 Comments

For illustration, check this out: tutorialspoint.com/java/character_islowercase.htm
@BasilSindhu Please accept the answer if you think it is correct.
0

If you know your model is always going to be either uppercase or firstcap can't you do something like this:

  public static String matchCase(String model, String source){ 
    if(model.toUpperCase() == model)
      return source.toUpperCase();
      // capitalize the first letter of source and send back 
   return Character.toUpperCase(source.charAt(0)) + source.substring(1);   
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.