Palindrome Detector Overview
KevinSarav
Posted on March 28, 2019
Today, I will be discussing the challenges I faced making my Palindrome Detector that I mentioned in my last post.
It turns out that the professor actually wanted us to implement the concept of a stack into this project. I couldn't simply work off of one String. That didn't make things too challenging because the professor gave us the outline for a Stack class that I just had to use.
One challenge I faced was trying to put a file as a parameter so I can check to make sure it can be opened first before making a new Scanner. I wanted to do this in a separate method so as to keep my main method clean. I found a post online that showed how to do it using InputStream in the method's parameter and new FileInputStream in the call, but an error message popped up. I ended up putting my file checker in my main. I'm sure there has to be some way to do it, but I couldn't find any. If you guys have any advice, I would greatly appreciate it
Another challenge I faced was when I was making my reverse stack method (makeReverStack). Initially, I decided I wanted to use the forward stack as one parameter since I already have it, anyway. I popped the values from it and pushed it to the reverse stack. However, an error message kept popping up saying I was going out of bounds of an array. I noticed through extensive debugging putting prints in different places and checking the values of variables that my top variable for my forward stack is -1.
I first checked to see where the error was occurring. I checked to make sure both of my stacks were filled. I found out one wasn't. I then checked to see if they had what they needed in them. They did. I checked to see how many iterations were happening with the loops. There was the appropriate amount. I checked that each had something in it. They did. Except the isFull method said otherwise (tying back into that top variable). It seems this was the result of popping. I assumed I could just use the forward stack from the parameter since it's just a separate object from the main, but it seems not. I think the Java might have made both objects (the one in my method and the one in my main) point to the same reference so that what I do in the method to the stack affects the one in my main.
To solve this, I decided to avoid using the forward stack and the pop method that affects the top variable. Instead, I just repeated the same thing I did with the forward stack method (makeLetterStack), except going backwards. I used the lettersOnly String instead of using the forward stack. I believe there is a way to circumvent this issue by using the .this operator from what I discovered online, but I don't have enough experience using it and I wanted to keep things simple since the forward stack method was already using the lettersOnly String.
The last big challenge I faced was trying to find out why my program was saying every line is a palindrome. I knew this error had to be occurring in my last method (printIfPali) because that's where the checking happens between both stacks and the printing occurs. I checked to see what the value of my boolean variable was. When I saw the variable was behaving as it should be, I checked my last if statement. I played around with the equal sign and found that a double equal sign made my program work fine while one equal sign made it work incorrectly. I believe this is because the = sign is supposed to be used when making something equal to something else. However, in an if statement, you should be using == to compare values.
In my next post, I plan to discuss my progress on my Calculator project and discuss any new things I've learned for my side group project and for my Software Engineering final project. I want to try learning some SQL, Angular and Apache.
Posted on March 28, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.