• 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
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Java code within a string.

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I am trying to embed some java code into a String value, but have run into some trouble. The code is as follow:


I am sorry if this is an old fashioned way of doing things. What I am trying to achieve is to place the row in a loop.
However I am getting errors as follow:


illegal start of expression


bad operand type String for unary operator '+'


not a statement




';' expected


not a statement



Really hope someone can help. Thanks.


 
Marshal
Posts: 73267
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The + operator requires either
  • 1: A number (primitive) on each side, or
  • 2: A String on one side and something else (any type, in fact) on the other.
  • What you have is a String on one side and a statement on the other. The compiler won't like that. Not at all.
     
    Campbell Ritchie
    Marshal
    Posts: 73267
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All your compiler errors probably arise from the same error. You would either have to use a loop and a StringBuilder:-Don't use \n unless you have been told an LF character is required. The line end constant can go in another class if you wish. I think you only want </table> once. Because many of the methods of StringBuilder end with return this; you can chain method calls like that.

    … or …
    There is bound to be a much more elegant way to do that with a Stream. But I can't remember how to do it.
     
    Campbell Ritchie
    Marshal
    Posts: 73267
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can also try a StringJoiner.
     
    Campbell Ritchie
    Marshal
    Posts: 73267
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you use Java7 or earlier you would have to use the StringBuilder method.
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    John Paterson wrote:I am sorry if this is an old fashioned way of doing things...


    Actually, it looks very "functional" to me; I'm quite impressed. It's Java that's old-fashioned (at least up to version 8).

    Building up chunks of HTML on your own, on the other hand...

    Winston
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic