1

I'm doing an assignment that takes two unlimited unassigned integers, represented as strings, and adds them up. My add method is giving me grief. I keep getting this error and don't know why.

Here's the part of the code giving me trouble:

for(int i = 0; i <= cap; i = i + 1){
    number.charAt(i) = number.charAt(i) + secondNumber.charAt(i);

    //loop checks to see if the current number at i is > or = to 10
    //if it is it subtracts ten from the character at i and adds 1 to the
      character at i + 1
    if(number.charAt(i) >= 10){
        number.charAt(i) = number.charAt(i) - 10;
        number.charAt(i + 1) = number.charAt(i + 1) + 1;        
    }   
}

Here's the error:

UUI.java:25: error: unexpected type

number.charAt(i) = number.charAt(i) + secondNumber.charAt(i);
                  ^
required: variable
found:    value

I get the error three times whenever I use the charAt method

3
  • where do you declare your variable Commented Feb 20, 2016 at 22:33
  • This is all within a class called UUI. An instance of this class is just a globally declared string and the various methods. This method in particular accepts another UUI as a parameter and then takes the string from that UUI and stores it in the local variable secondNumber. Commented Feb 20, 2016 at 22:45
  • "unassigned" — do you mean "unsigned"? Commented Feb 22, 2016 at 9:40

5 Answers 5

3

You can do one thing,if you don't want to go with StringBuilder better you convert your string into character array.by using `

yourString.toCharArray()

` and then you can assign any value at the particular index.i don't understand about cap which user has provided in the question but this is the concept how to get rid of such type of error.

String number,secondNumber;
char[] c1=number.toCharArray();
char[] c2=secondNumber.toCharArray();
for(int i = 0; i <= cap; i = i + 1){
c1[i] = c1[i] + c2[i];
.....//do rest of code}
Sign up to request clarification or add additional context in comments.

Comments

2

required: variable
found: value

The error is exactly what it states: you cannot assign a value to another value. You must write a value to a variable.

int a = 14; // Correct; write value "14" to variable "a"

char b = "somestring".charAt(4); // Correct; write result of method 'charAt()'
                                 // of class String to variable "b"

str.charAt(4) = "a".charAt(0);   // Incorrect; you cannot write a value to
                                 // the result of a method.

Some answers state that the error occurs because Strings are immutable. It is not true that that is the cause of the error. charAt(int) returns a value, and it doesn't make sense to assign something to that value. What should the machine do with the value? It needs to be assigned to a variable.

If I understand correctly, you want to re-set the char at the given position. You might need to use the StringBuilder, because a String is indeed immutable — you cannot change the characters. Just grab the code from saka1029's answer.

Comments

0

Strings in Java are immutable. You can't use charAt for assignment particular char inside String.

4 Comments

Ok, how would I get around that then?
Use StringBuilder for instance
Will do. Much appreciated.
This is correct, but not the "real" problem. His code fails, because the left hand side of the assignment is not a variable. One can't assign something to the returned value of a method.
0

Use StringBuilder like this.

    StringBuilder builder = new StringBuilder(number);
    for(int i = 0; i <= cap; i = i + 1){
        builder.setCharAt(i, (char)(builder.charAt(i) + secondNumber.charAt(i)));
        if(builder.charAt(i) >= 10){
            builder.setCharAt(i, (char)(builder.charAt(i) - 10));
            builder.setCharAt(i + 1, (char)(builder.charAt(i + 1) + 1));        
        }   
    }
    number = builder.toString();

1 Comment

Thanks, would I have to change the declaration of all of the strings in this program in the same manner as "builder"? I'm a bit of a neophyte here.
0

First of all you cannot add caracters, tecnically you can but what you obtain is the add of their ASCII code, I dont think you re looking for this.Also when you charAt() a String, you only obtain a character, so if your number has more than what digit the app will behabe unexpectly.

I would break the String into an array of characters, sustitute the ones needed and assemble them into a String again.

1 Comment

I might do that. Thanks

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.