5

I am trying to use the following code so that given a string, give the length of the longest contiguous subsequence of the same character. I am getting the error "incompatible types: char cannot be converted to java.lang.String". I have commented where the error is being found below.

public class Test {
    public int longestRep(String str)
    {
        int currLen = 1;
        String currLet = "";
        String maxLet = "";
        int maxCount = 0;
        int currPos = 0;
        int strLen = str.length();
        for(currPos = 0; currPos < strLen; currPos++)
        {
            currLet = str.charAt(currPos); //error is on this line
            if(currLet = str.charAt(currPos+1))
            {
                currLen++;
            }
            else
            {
                if(currLen > maxLen)
                {
                    maxLen = currLen;
                    maxLet = currLet;
                    currLen = 1;
                }
            }
        }
    }
    public static void main(String args[])
    {
        longestRep("AaaaMmm");
    }
}
6
  • 2
    That's because a char can't be converted to a String - I'm not sure what there is to not understand. Commented Mar 26, 2015 at 3:33
  • sorry about that, I'm a newbie to Java and am still learning syntax etc... Commented Mar 26, 2015 at 3:37
  • 2
    You could change String currLet = ""; to char currLet = ' '; and String == String is not how String comparison works in Java anyway... Commented Mar 26, 2015 at 3:38
  • I have used this solution @MadProgrammer , but now I have an error on the next line saying "incompatible types: char cannot be converted to boolean" Commented Mar 26, 2015 at 3:40
  • @Hugo That's because if(currLet = str.charAt(currPos+1)) - = is an assignment, not a comparison, you are trying to assign the value of str.charAt(currPos+1) to currLet which results in a char, but if is looking for a boolean result ... Commented Mar 26, 2015 at 3:48

4 Answers 4

11

String.charAt(int) returns a character. But currLet is of type String, so you can't assign a character. Use currLet = Character.toString(str.charAt(currPos)); instead.

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

6 Comments

Thanks so much for that! Now I seem to have the same error on the very next line... Do I use the same solution for this?
@Paul This is not how String comparison works in Java. You've thrown the OP out of the pot and into the fire...
@MadProgrammer who is comparing anything? this is just a solution for converting a character into a string.
@Paul The next if statement. if(currLet = str.charAt(currPos+1)), which is where your "example" code comes from...
@MadProgrammer thats another type of error... . i only provided a soution to the typeissue. and never said anything different.
|
5

As the compiler said, you can't convert a char to a String. If you have a char and you really want to convert it to a String of length 1, this will work:

String s = String.valueOf(c);

or

String s = Character.toString(c);

However, if the character you're working with was obtained by charAt, another solution is to get rid of charAt and use substring to return a string of length 1:

currLet = str.substring(currPos, currPos + 1);

Comments

3

Easy way to convert Char to String

Add an empty String to the start of expression, because adding char and String results into String.

Convert one char "" + 'a'
Convert multiple chars "" + 'a' + 'b'

Converting multiple chars works because "" + 'a' is evaluated first.
If the "" were at the end instead you would get "195"

Remember that Java language guarantees (Java Language Specification, Java SE 7 Edition, section 15.12.4.2) that all arguments are evaluated from left to right (unlike some other languages, where the order of evaluation is undefined)

Comments

2
  • currLet = str.charAt(currPos); A String value can't be assigned to a char, they are different types, apples and oranges
  • if (currLet = str.charAt(currPos + 1)) { is actually an assignment (make currLet equal to the value of str.charAt(currPos + 1))
  • if (currLen > maxLen) { - maxLen is undefined
  • You never return anything from the method...

Try changing:

  • String currLet = ""; to something more like char currLet = '\0'; and String maxLet = ""; to char maxLet = '\0';
  • if (currLet = str.charAt(currPos + 1)) { to something like if (currLet == str.charAt(currPos + 1)) {
  • Add int maxLen = 0 to your variable declerations (may be under int maxCount = 0)

Now, based on your example code, public int longestRep(String str) { will need to be public static int longestRep(String str) { in order for you to call from you main method...

5 Comments

This fixed all my errors, except I am unsure now where to return the value I need, and which variable I need to return
My "guess" is you want to return maxLen, based on the fact that you method is declared as returning an int. ps- You will get a runtime exception, so be prepared for some debugging.
@Hugo Also, it will do you no good to return maxLen if you throw away the result when you call your method. Make sure you assign the result to something, or do something else with it, when you call longestRep.
Thanks @MadProgrammer now the only error I have is index out of range in the second line of the for loop. What code should I use to avoid going out of range for the charAt? Thanks so much for all your help.
@Hugo Maybe something like for (currPos = 0; currPos < strLen - 1; currPos++) {. You will have to do another check for if (currLen > maxLen) { in case the last character is part of the sequence which is longer then anything else...

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.