• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringX challenge

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying below challenge
https://codingbat.com/prob/p171260
i wrote code as below


I see error as below

Compile problems:


not a statement line:2


Can you please point me what mistake i made here
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post comments on what the code is expected to do.  No link to another site.
Have you tried playing computer with a piece of paper and a pencil to see what that code does?

not a statement line:2


Note: That error message was not in the post 9 hours ago.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
looks like apparently i passed all the tests here. thank you. I removed the external site link as you suggested
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . . . I removed the external site link as you suggested

Please don't edit posts after they have been replied to. We now can't tell what the code is supposed to do; I am going to revert the edit.
Have you worked out what you got right? What on earth does line 4 mean?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:. . . please point me what mistake i made here

You probably already know you used a keyword in an incorrect context. Using new2 as an identifier name doesn't count as good coding style, however.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should remember that Strings are immutable, but like some other immutable datatypes it has a mutable counterpart. You will find your code much easier to understand and write if you use that mutable counterpart.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. StringBuilder and StringBuffer right.

My code worked as below now



But new2 not a good variable name probably temp is okay. I initially used new which is java reserved word and keep wondering why compilation error coming. This is what happens when I do not code daily at least for 1 hour including holidays and weekends. How else you all are keeping up with coding skill
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Thank you all. StringBuilder and StringBuffer right.

StringBuilder full stop. You should hardly ever have reason to use StringBuffer.

. . . new2 not a good variable name probably temp is okay. . . .

No, I would say temp is only slightly better.I shall let you work out the missing bits of code. They are all simple and easy to understand.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thank you. I use String Builder and ignore StringBuffer
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what did you replace my ??? with?
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not chance to think deep, work on that. I will try to think and work on that logic and let you as soon as i make some progress.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i wrote my code as above but getting timeout errors.

can you please point me to my mistakes here?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 6 and a half, put:

Can you see what the error is?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree with your solution:-
  • 1: Don't append a 0‑length String to your StringBuilder. That does nothing. You don't, if possible, want control structures which do nothing. You could simplify the control structure to get rid of the else and still have the program work correctly.
  • 2: Have you worked out why you are going into an infinite loop yet? The answer is quite simple.
  • You appear to be guessing at solutions. If you read the API for the classes we mentioned, and if you planned what you are going to do before writing any code, you would get correct solutions so much faster.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:. . . You could simplify the control structure to get rid of the else and still have the program work correctly. . . .

    That would make the solution more complicated than I was thinking of at first, so I suggest you stick with the else.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    above passed some tests bug failing few. Checking them now

     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Above passed all tests but looks long. Any opportunity for refactoring or optimizing this code?
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Write down what the state of your StringBuilder will be before you start the loop, and again after every iteration of the loop. That, I hope, will show you what you are getting wrong.
     
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:Above passed all tests but looks long. Any opportunity for refactoring or optimizing this code?


    Forget optimisation. Start from making it readable first.

    Can you extract that code to multiple methods, so it is clear what the program should do at a hight level.

    Example:

    As a code reader, I'm not interested in the nitty gritty details at the very first time I see code. All I want to see is what the code is supposed to be doing. If there are any interesting parts how they are implemented, I'd know how to locate them.

    See an idea and the difference from your code?

    Now when everything is clear and working, it may appear that to putShoesOn() it takes way more longer than it normally should. So then you go there and check why it takes for so long - then you are looking how to optimise it and make it more performant.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For this challenge could not think of any helper methods though. May be finding length of str one helper method?
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:For this challenge could not think of any helper methods though. May be finding length of str one helper method?


    I don't know. Maybe there isn't really. Could be that my suggestion after all wasn't that great. But keep this suggestion somewhere close by for some other problems
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:. . . May be finding length of str one helper method?

    Maybe, and maybe not. Think again what helper methods you might like to use.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:. . . . Maybe there isn't really. . . .

    What about a method to tell whether the current char is 'x', or to return a one‑letter String depending on whether you do or don't have an 'x'?
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes first time i ever know difference between readable and optimized/performance code in layman simple terms. I will try to keep in my mind
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:. . . difference between readable and optimized/performance code . . .

    At a first approximation, there is no difference. The simpler and more readable the code is, the better the JVM can optimise it. Remember that automatic tools can optimise code much better than programmers ever can, because the tools can “see” where the bottlenecks are at runtime.
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @OP

    Since you got your version working, here are mine, just for variety reasons Second it was just for fun, Campbell challenged me to cram all the solution to one statement.

    By the way, I like my 1st more for readability reasons.

    1st

    2nd
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    even more readable
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't know whether you realised this, but we mods have been amusing ourselves by trying to solve this challenge and sending each other the results. Some of them are pretty horrible. This example, written when I suggested to Liutauras that we try using only one return and the ?: operator, is I think pretty bad style. If you mess around with the indentation so the ?s and the :s no longer align vertically, the code will degenerate into complete illegibility Although Liutauras published his solution first, I showed him the following about 1 minute before he showed me his code. If you go through the two suggestions, you will find they are actually very similar to each other.Please go through the two solutions with ?: because that is a useful operator which many beginners, myself included, don't use enough. It can make for concise code, or with careless indentation, incomprehensible code.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The first following suggestion dispenses with the else, but it is inelegant; it requires two indices one for source (old) and one for destination (new). That is the advantage of using a forum. I easily managed to pull the wool over CodingBat's eyes because I got the “correct” results, but the real people reading my code would have told me off about it:-Notice the use of insert() to add letters in the middle of the text.

    The following uses Streams (well actually IntStreams). I think it is inelegant code because it is necessary to traverse the text thrice, once for the first letter, once for the middle of the text, and once for the last letter. Note the helper method:-The following is an earlier attempt, and this is one I do think is good. It uses the fact that the first and last letters are left unchanged, and only xs are removed from the middle of the text. The else allows for the fact that you only move to the next letter if you don't have an x. If you do have an x, the StringBuilder becomes one letter shorter and the next letter comes to me.Take good note of Piet's methods, which are much more concise.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Interesting. To my eyes java 8 syntax looks less code but more hard to read. May be i have not used it often enough
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:. . . May be i have not used it often enough

    You should use Streams. Get a copy of Java8 in Action or Modern Java in Action (different editions of the same book) by Urma Fusco and Mycroft (Manning) and go through that.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Checking Modern Java in Action which has 592 pages. It is been many years i ever read technical book completely. Mostly i am following online videos these days. Do you guys read books these days any more? What other effective learning tools you all using to keep up to date
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    sai rama krishna wrote:. . . 592 pages. . . .

    I have books with lots more pages

    Do you guys read books these days any more? . . .

    Yes.
     
    sai rama krishna
    Ranch Hand
    Posts: 930
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I will read more and practice modern java. thank you
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I think it is inelegant code because it is necessary to traverse the text thrice


    It can be done in one go, but it is still inelegant. For instance:

    Here I did the static imports Stream.of, Arrays.stream, Stream.concat and Stream.joining().
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic