• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Peter Rooke
Bartenders:

compile error

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 6307
507
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic