Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Recursive boolean method for alternating characters in a String.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I am new in java. I am trying to check if a string consists of alternating charachters with recursive using helper method but getting error. Help me please.
This is my code:


 
Rancher
Posts: 157
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The first check here .charAt(0) is safe, because you can't have a String of zero length because you checked it with an if statement.
How can you be sure that s has a character at index 1?
Remember that Java is zero-indexed, so index 0 is the first character. Index 1 is the second character.
Can you always be sure that this String s has two characters? What if it only has one?

This check:

Only happens the first time, since this code is in the public-facing method, not the private helper method that you use.
When you trim characters off of the String (using substring) the length changes.

It's useful if you post the error message you get here as well. Just saying that you get an error doesn't help much.
The error message can also give you a hint as to what's going wrong so that you can solve it yourself. Like in this case, the error probably tells you that index 1 is out of range.

I also recommend that you include the start brace and end brace around your if/else statements. This can help avoid logic errors if additional lines are added to that if/else statement.
 
Abinn Gautam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Zachary,
Smart thinking. Since i have been testing this in junit test, i get the following error.
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
 
Rancher
Posts: 4548
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means your test is failing.
It's not a very good assertion in my opinion since it doesn't tell what has failed, you need to look at the actual test code to find out.
 
Marshal
Posts: 68908
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through your isAlternating() method and explain what each line does. Now you know you have an error, you need to find it, which means dissecting your code.
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, show us your JUnit tests so we can see if there's something you can do to make it easier to find what's wrong with your code.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're actually not very far from a fully working solution. When dealing with recursion, you have to think of how the data you're dealing with gets reduced to the degenerate or trivial cases.

Hint: The values you pass to the next recursive call is typically different from what you have in the current call. Are you doing that?
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll share the JUnit tests I wrote to solve this problem. These tests were written for JUnit 5 but if you're using JUnit 4, all you have to do is move all the @Test methods under the main Test class and delete the @Nested classes. Of course, you'll want to use the JUnit 4 imports instead as well.

The solution I came up with is based on what you posted and consists of 19 lines, including spaces and the package declaration.
 
Abinn Gautam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu, Many thanks for your response.
Hint: The values you pass to the next recursive call is typically different from what you have in the current call. Are you doing that? Yes by providing a substring.

Here is my test method.
@Test
   public void isAlternating() {
       StringRecursion sr = new StringRecursion();

       assertTrue( sr.isAlternating( "" ));
       assertTrue( sr.isAlternating( "a" ));
       assertTrue( sr.isAlternating( "ab" ));
       assertTrue( sr.isAlternating( "aba" ));
       assertTrue( sr.isAlternating( "ababa" ));
       assertFalse( sr.isAlternating( "aa" ));
       assertFalse( sr.isAlternating( "abc" ));
       assertFalse( sr.isAlternating( "abcba" ));
       assertFalse( sr.isAlternating( "abaaba" ));
       assertFalse( sr.isAlternating( "aabb" ));
   }
It gives error from the third test.


 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abinn Gautam - nice use of recursive.  I think the issue is the call to the recursive.  Line 25 should be changed from:
   return isAlternating(s.substring(1), first, second);
to
   return isAlternating(s.substring(1),  second,  first);
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the qualities of a good test suite is that each test should be independent of the other tests. A good test suite will help you figure out what exactly is the problem. Look at the way I structured my tests. If there's anything wrong, I can quickly zero in on what the mistake is because the failing test will be very specific. It's best to only have one or a few related assertions in a test method. You have all your assertions in one test method and it's not as easy to figure out exactly what's going wrong. Which line are you referring to as your "third test"? What is the error? Just looking at your code, I would guess it's an IndexOutOfBoundsException.
 
Abinn Gautam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is solved. Thanks guys.
 
Campbell Ritchie
Marshal
Posts: 68908
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and what was the solution?
 
Abinn Gautam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the solution:
private boolean isAlternating( String s, char first, char second ) {
       if (s.length() == 0)
       return true;
       if(s.charAt(0) == first){
               return isAlternating(s.substring(1),second,first);
       }else
       return false;
       }
 
Campbell Ritchie
Marshal
Posts: 68908
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good
You can probably shorten that method however.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's the essence of it but it can be more concise:

or if you prefer

or if you really hate formula expressions:

and finally, you could change the signature a little to take advantage of autoboxing:

I actually prefer this last version for some reason.
 
Hey cool! They got a blimp! But I have a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic