• 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

Finding and printing the longest unbroken alphabetical string hidden in another string!

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


Can the code above be improved? What it does is, traverse through an unordered string, find the longest hidden alphabetically ordered string and print it.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 34 is redundant.
Line 41 is redundant.
Use the String class's valueOf method on line 42 to convert a char to a String.
 
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you are looking for. I can see, “abcdef,” which I think is an “unbroken alphabetical” substring, and I can also see, “bd'” which I think probably isn't “unbroken alphabetical”.
Please only post the code you are actually running; there is no point in posting commented‑out code. That makes the code very difficult to read.
Please format and indent the code correctly.
After an isBlank() call, there is no need for an isEmpty() call (line 24).
Line 29 is confusing. It looks as though you had stumbled upon a very inefficient way to iterate the String.
 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@NormRadder
                       Line 34 is finalStr = "";
                       Line 41 is subStr = "";

        Why do you say they are redundant?

       

Use the String class's valueOf method on line 42 to convert a char to a String


                          Line 42 is
                                            The character in s.charAt(i) gets concataneted to the subStr. So why do you say to use the valueOf() of the String class to convert char to String?

@CampbellRitchie
                                 

After an isBlank() call, there is no need for an isEmpty() call (line 24).


                                               Are they are the same?
                             

 Line 29 is confusing.


                                            Line 29 is
                                                      What line 29 does is compare the last character in the subStr for alphabetical order with a character in the string that's currently going through the loop.
 
Saloon Keeper
Posts: 10780
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say we were dealing with int variables 'i' and 'j', if you wrote this
What would be the point of having line 1 if you are just going to throw the value away and replace it with the contents of 'j' on the very next line?

You have the same issue but with String variables.
 
Carey Brown
Saloon Keeper
Posts: 10780
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Campbell's comments about cleaning up your code.
 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@CareyBrown
                        Line 2 is a test to see how the program works if the whole string is alphabetical
 
Carey Brown
Saloon Keeper
Posts: 10780
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why you are going through all those painful steps with sub-strings. You could more easily use two indexes into the input String: begin and end. Increment these as needed and use charAt(idx) to compare the characters. The longest distance between begin and end would be your answer.
 
Carey Brown
Saloon Keeper
Posts: 10780
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varuna Seneviratna wrote:@CareyBrown
                        Line 2 is a test to see how the program works if the whole string is alphabetical


Huh?
Line 2 is
i = j;
 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey Brown

Huh?
Line 2 is
i = j;



Line 2 was
        This is just a test I ran to check if the program works correctly if the whole string is alphabetical
 
Carey Brown
Saloon Keeper
Posts: 10780
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Let's say we were dealing with int variables 'i' and 'j', if you wrote this
What would be the point of having line 1 if you are just going to throw the value away and replace it with the contents of 'j' on the very next line?

You have the same issue but with String variables.

 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I don't understand why you are going through all those painful steps with sub-strings. You could more easily use two indexes into the input String: begin and end. Increment these as needed and use charAt(idx) to compare the characters. The longest distance between begin and end would be your answer.



Can you please explain what you mean in

You could more easily use two indexes into the input String: begin and end. Increment these as needed and use charAt(idx) to compare the characters.



How to use two indexes into the input String?
 
Carey Brown
Saloon Keeper
Posts: 10780
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just to show you how to use indexes and charAt().
 
Campbell Ritchie
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varuna Seneviratna wrote:. . . Are they are the same?

Look at their documentation.

. . . What line 29 does is compare the last character in the subStr for alphabetical order with a character in the string that's currently going through the loop.

That doesn't add to the explanation, I am afraid.
 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please don't edit old posts; that makes the thread difficult to follow and makes the line numbers appear wrong. I am refusing the first edit.

Please post the cleaned up code as a new post.




I was responding to your reply. This is your reply I was referring to.

Campbell Ritchie wrote:Please explain what you are looking for. I can see, “abcdef,” which I think is an “unbroken alphabetical” substring, and I can also see, “bd'” which I think probably isn't “unbroken alphabetical”.
Please only post the code you are actually running; there is no point in posting commented‑out code. That makes the code very difficult to read.
Please format and indent the code correctly.
After an isBlank() call, there is no need for an isEmpty() call (line 24).
Line 29 is confusing. It looks as though you had stumbled upon a very inefficient way to iterate the String.

 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Varuna Seneviratna wrote:. . . Are they are the same?

Look at their documentation.

. . . What line 29 does is compare the last character in the subStr for alphabetical order with a character in the string that's currently going through the loop.

That doesn't add to the explanation, I am afraid.



This is line 29

What line 29 does is check for alphabetical order the last character in the subStr against a character in the string.  The character of the string which is being compared is the character currently going through the loop to be checked for alphabetical order.

The Documentation for String.isEmpty says Returns true if and only if the length() is 0 and for String.isBlank() says Returns true if the string is empty or contains only whitespace codepoints,otherwise false.  Thanks, Campbell

At the time I didn't know whether for whitespace the length() is 0 or not.  The length() is not 0 for whitespace, isn't it? and isEmpty() returns false for whitespace. I was not sure how both methods work at the time, that's why I included them both

Maybe the description I gave is confusing what about the following

 
Campbell Ritchie
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varuna Seneviratna wrote:. . . I was responding to your reply.

Sorry if I wasn't clear.
reply
    Bookmark Topic Watch Topic
  • New Topic