Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Determining whether an input is an alliteration.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Do you have a question about that code?
 
Andrew Bauer
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am just wondering what would be the most efficient method of grabbing the first letter of each word in a string and checking to see if those letters match each other? I feel like maybe I'm missing something obvious or maybe not. If the letters match each other the output should tell the user that they entered an alliteration, otherwise tell them that it isn't an alliteration.
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really want all the initial letters to be the same? Most alliterations I have read have three or four words with the same initial. It is however much easier to test for every word having the same initial. I can't understand your loop to find alliteration. Don't try printing something in such a loop unless you want multiple outputs saying it is an alliteration followed by one saying it isn't.

I can think of several ways to do it:-
  • 1: Iterate the text and find the first letter after each space.
  • 2: Use the String#split() method.
  • 3: Pass the entire line to a Scanner's constructor and use its next() method to find words. I think the default delimiter should be used unchanged.
  • There are bound to be other ways to do it.
     
    Andrew Bauer
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes that is the preferred technique for determining an alliteration that I plan to stick with for now. Would you consider changing the for loop into a while loop to perform the iteration?
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Andrew Bauer wrote:. . . . Would you consider changing the for loop into a while loop to perform the iteration?

    I wouldn't think about loops at all at this stage. You haven't yet worked out what you are going to do, but you are leaping ahead to implementation details. It might only take a few minutes to work out what you are doing, but that has to be done first.
     
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Andrew Bauer wrote:Yes that is the preferred technique for determining an alliteration that I plan to stick with for now.


    Preferred by whom? Does it even work? From what I can tell, it doesn't.
     
    Andrew Bauer
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:

    Andrew Bauer wrote:Yes that is the preferred technique for determining an alliteration that I plan to stick with for now.


    Preferred by whom? Does it even work? From what I can tell, it doesn't.



    You are correct that it does not work in the sense of the desired output. The current problem is that any input that I enter is identified as an alliteration even though it is not. That is what I am trying to fix without the program producing an infinite loop of output.
     
    Marshal
    Posts: 7264
    492
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Andrew Bauer wrote:Yes I am just wondering what would be the most efficient method of grabbing the first letter of each word in a string and checking to see if those letters match each other?


    I'd say, for a start, don't look for most efficient method. Look for most clear and simple to understand method. In fact - methods. You crammed all your logic to a singular method and added quite a few comments, which actually could act as hints, how many methods you should be aiming to have.

    The thing with comments is, that whenever you decide to change code (even slightly), you need to make sure comments are intact, and that is an extra work. Why not to substitute them with methods? Method names could convey an information what your comments tell.

    Once you decompose your problem into smaller problems, meaning different methods, where each of them would do a singular task only, you'd find much easier to cope with an infomation you have at hand.

    Give a try and tell us what you think?

    The structure you aim may look similar to:

    Notice lines 5 and 7, those could serve your code on lines 21 - 30 and 33 - 42 as they essentially do the same.

    Now, the structure and the methods names I showed as an example probably could be improved to a good extent, but that is what you could aim to improve gradually.
     
    Andrew Bauer
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator



    This is what I changed the previous code to for the alliteration part. It breaks the sentence into pieces but now I'm now stuck again on how to compare the first letter of each word.
     
    Liutauras Vilda
    Marshal
    Posts: 7264
    492
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In which method this code snippet you just showed resides after you decomposed your main() method into many small methods?

    All the parsed words from that method you could return for further actions you may need to take (yet with another method), for instance one of those actions could be:

    Andrew Bauer wrote:...how to compare the first letter of each word...

     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Andrew Bauer wrote:This is what I changed the previous code to for the alliteration part. It breaks the sentence into pieces


    Are you sure? Did you actually try it? Because I'm pretty sure that code will not compile.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, has anybody warned you about == true and == false? They are both poor style and error‑prone if you write = true etc. by mistake.
     
    Liutauras Vilda
    Marshal
    Posts: 7264
    492
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Andrew Bauer wrote:


    This is what I changed the previous code to for the alliteration part. It breaks the sentence into pieces but now I'm now stuck again on how to compare the first letter of each word.



    Have a look what Scanner's documentation says about such method:

    public boolean hasNext​(String pattern)
    Returns true if the next token matches the pattern constructed from the specified string. The scanner does not advance past any input.
    An invocation of this method of the form hasNext(pattern) behaves in exactly the same way as the invocation hasNext(Pattern.compile(pattern)).

    Parameters:
    pattern - a string specifying the pattern to scan
    Returns:
    true if and only if this scanner has another token matching the specified pattern
    Throws:
    IllegalStateException - if this scanner is closed

     
    Andrew Bauer
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'll admit I'm lost on how the logic would work for this part of the problem.
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Andrew Bauer wrote:I'll admit I'm lost on how the logic would work for this part of the problem.


    That's Ok, that happens to many people. Programming can be quite daunting and sometimes the challenge of learning a new language can build artificial walls in our brains that prevent us from thinking logically.

    As advised before, stop thinking about Java for a minute and think about how you would do this yourself. Try to tell the story in plain, simple terms like this:

    "To see if a sentence is an alliteration, I would check to see that each word in the sentence starts with the same letter. If they do, then it's an alliteration."

    Now you start to break it down, step by step:
    1. Take a sentence and break it apart into separate words.
    2. Note down what the first character of the first word is.
    3. Check all the words to see if they start with that same first character.

    Step 1 is easy to implement in Java using String.split()
    Step 2 is easy to implement in Java using an assignment statement and the String charAt()
    Step 3 is going to require some kind of loop. This is a little bit more involved (only a little bit) so you might want to break it down into smaller steps, like so:

    3a. For each word in the sentence (obtained from Step 1 above):
            If the word's first character isn't the same as the character from Step 2, then sentence is NOT an alliteration (no need to keep going at this point so just return what we found out)
    3b. If we went through all the words without finding a non-alliterative word, then all the words must be alliterative (return what we just found out)

    ====
    Study this example to get an idea for your problem:

    This checks if the given array contains all positive numbers:

    The code above tells this story:

    For each number given
       If the number is less than 0 then we know not all numbers are positive so stop here and report what we found
    Got through all the numbers, so they must all be positive. Report what we found.
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    I wrote:Step 1 is easy to implement in Java using String.split()


    If you're not allowed to use String.split() but have to use Scanner and Scanner.next() instead, that's fine. A Scanner will involve more code though and it will be awkward to use a String[]. I would use a List<String> if I had to use a Scanner to break down a sentence into its component words.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can also use a method to get a Stream<String>
     
    Andrew Bauer
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I feel like I'm relatively close now. Right now the program considers "Alliterations artful aid" as an alliteration, but completely disregards "Round the rugged rock the ragged rascal ran." and considers it to not be an alliteration. Here's the entire scope of the code again


     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You wrote this:

    First of all, it's not formatted properly, so it's difficult to discern the logic flow because everything is smushed together.

    Read the wiki page that Knute wrote up about why you should format code properly: https://coderanch.com/wiki/701738/Format-Code

    Here's what that code looks like formatted and indented properly:

    Just glancing at that makes it clear to me that you're only looking at the last word in the sentence to make the determination of whether the sentence is alliterative or not. That is clearly wrong. Furthermore, you conditional says this: If the first letter of the last word in the sentence is between 'Z' and 'a' (inclusive) then the sentence is alliterative.

    Here's a quick little snippet that will show you what characters are going to result in "The input is an alliteration" message from your code. You can run this in JShell:


    Here are some input sentences that your code will report as alliterative (try them out yourself):
    "[This shouldn't be alliterative"
    "^This is NOT alliterative"
    "_Neither should this be"
    "\\Neither should this but apparently it is"

    *(Edited based on Piet's feedback below)

    Andrew Bauer wrote:but completely disregards "Round the rugged rock the ragged rascal ran." and considers it to not be an alliteration.


    Even a broken clock is right twice a day. That's actually correct behavior because not all of the words in that sentence start with the same letter. You probably overlooked the fact that the word "the" doesn't start with the letter "r" and there are two of them in that sentence.  But while your program actually gives the correct results, it's just a happy coincidence. Your logic is still incorrect.
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When Campbell asked:

    Campbell Ritchie wrote:Do you really want all the initial letters to be the same? Most alliterations I have read have three or four words with the same initial.


    You replied:

    Andrew Bauer wrote:Yes that is the preferred technique for determining an alliteration that I plan to stick with for now.


    But then, you wrote:

    Andrew Bauer wrote:but completely disregards "Round the rugged rock the ragged rascal ran." and considers it to not be an alliteration.


    You're going to have to make up your mind here. Should your program logic require all words to start with the same letter to be an alliteration or not?
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By the way, when checking for lipograms, you might want to consider using String.contains() instead.
     
    Bartender
    Posts: 3606
    151
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu wrote:Just glancing at that makes it clear to me that you're only looking at the last word in the sentence


    No, it looks that way, but have a look at line 16:

    Print out original and see if you can determine an alliteration.

    But then there is the check of a palindrome as well.

    @OP
    How to solve this?
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That means he's only got one word in any "sentence" then so technically, the code is still only looking at the last word.

    But thanks for pointing that out.

    I had noticed the replace() call before but totally forgot about it when I replied, just focused on that particular section of code. I've edited my previous reply.  
     
    Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!