0

i'm fairly new to java and i'm trying to use indexOf to check if a persons name ends with the letters in the array and output a word it rhymes with. Where am i going wrong? e.g. Dean ends in "ean" so it rhymes with green. Thanks guys

    String [] first = new String [3];
    first [0] = "eem";
    first [1] = "een";
    first [2] = "ean";

    for (int i = 0; i < first.length; i++) {

        if (first[i].indexOf(first.length) != -1){
            System.out.println("That rhymes with green");
        }
    }
tim roth
  • 3
  • 3
  • see my code, let me know if I am missing something. Just run it directly on my machine, its working for me. –  Sep 11 '15 at 18:08

3 Answers3

2

To check with weather the input contains any array given element's, you should receive input and then iterate over your array to look at. For ex

  String personname = "Dean";
  String [] first = new String [3];
    first [0] = "eem";
    first [1] = "een";
    first [2] = "ean";

    for (int i = 0; i < personname.length; i++) {    
        if (input.indexOf(first[i]) != -1){  // check my input matched
            System.out.println("That rhymes with green");
        }
    }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Just to piggyback this. OP the reason your code is breaking is because you are testing the contents of your `first` array against the length of the array. `first[i].indexOf(...)` will cycle through "eem", "een", and "ean" and check to see if there is a character equal to the length. So you're checking in your code whether "eem" has a 3 in it and not whether the input has an "eem" in it. – Braains Sep 11 '15 at 17:29
  • How is the question's part addressed that OP wants to know if the input is *ending* with some of the rhymes in the other array? – Fabian Barney Sep 11 '15 at 17:31
  • @FabianBarney is right. The listed code would say that "Deanery" rhymes with "green". the OP should use endsWith() instead of indexOf(). – FredK Sep 11 '15 at 17:34
  • @FabianBarney OP stated that `i'm trying to use indexOf to check if a persons name ends with the letters in the array and output a word it rhymes with`, I may be wrong here too :) – Suresh Atta Sep 11 '15 at 17:34
  • If he wants to do indexOf to meet a requirement and can't use endsWith() then they can test indexOf(first[i]) == input.Length() - first[i].Length() – Braains Sep 11 '15 at 17:36
0

I have tested and ran it on the compiler. This is working fine. Please comment for any quesions. Thanks

import java.util.*;

public class HelloWorld
{

    public static void main(String []args)
        {
            String [] first = new String [3];
            first [0] = "eem";
            first [1] = "een";
            first [2] = "ean";

            /* I am trying to get the input from user here */

            String s;
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the string:");
            s = in.nextLine();

            /* Now, String.indexOf(substring) will check the condition if the match happens it will print the output, if it doesn't it returns -1 */

            for (int i = 0; i <s.length(); i++) 
                {    
                    if (s.indexOf(first[i]) != -1)
                        { 
                            System.out.println("That rhymes with green");
                        }
                }

       }
}
0

You should use endsWith instead of indexOf. indexOf will return the index where the passed string exactly matches the current string and, as the name suggests, endsWith will check if the current string ends with the passed string.

Take a look at the following code:

String personName = "Dean";
String[] suffix = {"eem", "een", "ean"};
String[] names = {"greem", "green", "grean"};

for(int i = 0; i < suffix.length; i++) {
    if (personName.endsWith(suffix[i])){
        System.out.println("That rhymes with " + names[i]);
    }
}

Also, ideally, you would want to keep a map of suffix -> name for maintainability, but for simplicity/exploring this should be fine.