• Post Reply Bookmark Topic Watch Topic
  • New Topic

compile error  RSS feed

 
simrat khanuja
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone please tell me what's wrong with my code
i keep getting this error message.

code is:

error is:
unexpected type
required: variable
found: value
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please use code tags when posting you code.

The problem is here:



You are trying to assign to a substring. This is a natural thing to assume you can do, but strings in Java are immutable, so you need to create a new string. Probably you need substring() or StringBuilder.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's nothing to do with the immutability of Strings. Method calls cannot be used on the left hand side of an assignment expression.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:It's nothing to do with the immutability of Strings. Method calls cannot be used on the left hand side of an assignment expression.


Okay, but why can't you have an expression on the LHS? In Perl and some BASICs you can do this:



Perl allows this because in Perl, strings are mutable. The reason Java doesn't is because strings are immutable.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Perl allows this because in Perl, strings are mutable. The reason Java doesn't is because strings are immutable.

I don't know how it works in Perl, but Joanne is right. It's nothing to do with the immutability of Strings.
In Java you just can not have a method call on the left side of an assignment operator.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Welcome to the Ranch!

As Knute advised, it is best to UseCodeTags (<- click link) when posting nicely indented code. Apart from it looking nice, it makes it much easier to read which in turn improves your chances of getting a good reply to your questions. I've added them for you this time, looks much better don't you think?

Regards, Tim
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The reason Java doesn't is because strings are immutable.

StringBuilders in java are mutable, but this code won't compile
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!