Results 1 to 5 of 5
  1. #1
    aejhyun is offline Member
    Join Date
    Aug 2015
    Posts
    2
    Rep Power
    0

    Default Using charAt on a string.

    I am getting the following message and I'm not exactly sure why. Any idea how to fix this?

    Java Code:
    MyClass.java:13: error: unexpected type
    					str1.charAt(i + 1) = str1.charAt(i); 
    					           ^
      required: variable
      found:    value
    1 error
    My code is:

    Java Code:
    public class MyClass {
    
    	public static boolean checkPermutation(String str1, String str2){
    		char temp; 
    
    		if(str1.length() != str2.length()){
    			return false; 
    		}
    		else{
    			for(int i = 0; i < str1.length() - 1; i++){
    				if(str1.charAt(i) > str1.charAt(i + 1)){
    					temp = str1.charAt(i + 1);
    					str1.charAt(i + 1) = str1.charAt(i); 
    					//str1.charAt(i) = temp; 
    				}
    			}
    			return true;
    		}
    	}
    
    	
    	public static void main(String[] args){
    		if(checkPermutation("heello", "helelo")){
    			System.out.println("comparing strings work!"); 
    		}
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using charAt on a string.

    A method invocation can't be the target of an assignment. So you can't assign anything to str.char(i+1). And even if you could, strings are immutable so it would not be allowed. You should convert the String to an array of characters and manipulate the array or check out StringBuilder which allows individual character changes.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: Using charAt on a string.

    What Jim is saying is that "str1.chatAt(i + 1)" results in a value but this value does not point to the thing where that value came from. In other words, you are trying to treat this functionally as if:

    s(i + 1) = s(i)

    but we are not talking about equations here. What you are trying to do is variable assignment and that is not a mathematical formula. There is no way in Java to assign something to a string using the charAt method; it only yields a value, but cannot be assigned to.

    What the compiler error tells you is that it expected a variable since a variable is the target of an assignment. Any form of alteration of a string like that (or anything) in Java that I'm aware of would require a method invocation like "stringBuilder.setCharAt(int index, char ch)" where both the index AND the character are parameters to the method (function).

    So even then, with a StringBuilder, you can't, and won't be able to do "stringBuilder.setChar(index) = ch". It doesn't work. You can't assign values to methods. Methods produce values, you can't direct anything towards it like that.

    Also, the routine as you have constructed it will do the following.

    • assign the value of position 0 to position 1
    • assign the value of position 1 to position 2
    • and so on


    This means you are just overwriting the entire string with the value of the first character. If what you want to do is move everything back (i+1 becomes i) then that could produce something useful, it means you would shift the entire string left 1 position and discard the first character, but there are of course easier ways of accomplishing that ;-). For example, in that case the subString method as here would copy the entire thing in one go.

    But I understand as you are trying to learn the gripes, I wish you well in completing this assignment or chosen project ? ;-).

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using charAt on a string.

    Why do feel the need to explain my explanation? Would you at least wait until the OP asks for clarification?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: Using charAt on a string.

    Because it's fun. And because it is clear that your two-line explanation will be hard to understand by the question-asker. And because I have the time to do so. And because I can't help but notice that there is a race on this forum to be the first to answer a question, which is also indicated by your question and criticism just now. And because I wanted to provide an answer anyway. Why should your first, short answer, be the only one? So now I am here wanting to write a lot, but I can't because you have already provided one? It was not as much as explaining your answer (even though that credits you) but more about providing my own, and I like to help with dedication and to provide something that a user or learner will find a bliss to read and easy to understand. I just write what I would like to read myself. Can I not?

    This is again that we are having trouble with each other :p. I don't really know what your issues are, but I'm not doing this for you or about you, I scarcely have any issue around you, and I just love writing. I'm sorry if I offend by doing what you have done or seeming to be trying to "best" you, maybe that is true. I don't know. I just wanted to provide an answer and there already was one but I just went ahead. That is all there is to it. A different perspective yields a better explanation and two perspectives add to one another, they don't diminish each other, naturally, unless you start to break down what the other has written or said.

    Can't we just get along? lol.

Similar Threads

  1. Replies: 2
    Last Post: 07-05-2012, 08:10 AM
  2. Str.charAt() ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 21
    Last Post: 08-23-2011, 07:42 AM
  3. charAt problem... Please help
    By Matija in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 01:20 AM
  4. Help with charAt()
    By HackerOfDoom in forum New To Java
    Replies: 7
    Last Post: 03-21-2010, 05:27 PM
  5. using String.charAt() for semicolons
    By porchrat in forum New To Java
    Replies: 5
    Last Post: 01-11-2010, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •