CaptainMuffenz
CaptainMuffenz

Reputation: 81

Trying to find index of string in array in java

I am trying to find the indexOf the longest string in the array, which is represented by the string longString. I keep getting a "cannot find symbol" error and have no clue how to fix this

public class Final21 {
       public static String getLongString(String[] array) {
          int x=0;
          int maxLength = 0;
          String longString = null;
          for (String s : array) {
              if (s.length() > maxLength) {
                  maxLength = s.length();
                  longString = s;
              }
          }
          return longString;
      }

         public static String getShortString(String[] array) {
          int minLength = 0;
          String shortString = null;
          for (String t : array) {
              if (t.length() > minLength) {
                  minLength = t.length();
                  shortString = t;
              }
          }
          return shortString;
      }

      public static void main(String[] args) {
          String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
             String longString = getLongString(names);
                System.out.println("The Longest String is: " + longString + " With The Index Of" + names.indexOf(longString));

             String shortString = getShortString(names);
                System.out.println("The Longest String is: " + shortString + " With The Index Of" );
      }

    }

Upvotes: 2

Views: 10759

Answers (3)

MinA
MinA

Reputation: 405

Syntax of the indexOf() is:

public int indexOf(char ch);

It returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.

To get the required output.

You can try to change your code with following;

public static String getLongString(String[] array) {
     int x=0, maxLength = 0, index = 0;
     String longString = null;

     for (String s : array) {
         if (s.length() > maxLength) 
         {
             maxLength = s.length();
             index++;
             longString = s;
         }
     }
 return "The Longest String is: "+ longString + " With The Index Of :" + index;
}

 public static void main(String[] args) {
       String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
       String longString = getLongString(names);
       System.out.println(longString);

 }

Upvotes: 2

Romeo Sierra
Romeo Sierra

Reputation: 1756

Problem lies in names.indexOf(longString). Because names is of type String[] which is an array. This type String[] does not have a method definition named indexOf. As an alternative you can try java.util.Arrays.asList(theArray).indexOf(o)

So, to correct your code snip, you could rewrite it like

System.out.println("The Longest String is: " + longString + " With The Index Of" + java.util.Arrays.asList(names).indexOf(longString));

Read the JavaDoc for java.util.Arrays to brush up your knowledge on how to deal with arrays in java, using Java API.

Further, you can achieve the same by modifying the semantics of your code. This answer by Elliott Frisch has done that for you. Read that as well..

Upvotes: 7

Elliott Frisch
Elliott Frisch

Reputation: 201419

There isn't an indexOf function in arrays, instead of your current approach - I would return the index from the getLongString and getShortString methods; start by assuming it's the first element. If any element is longer (or shorter), update the return value. Like,

public static int getLongString(String[] array) {
    int max = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i].length() > array[max].length()) {
            max = i;
        }
    }
    return max;
}

public static int getShortString(String[] array) {
    int min = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i].length() < array[min].length()) {
            min = i;
        }
    }
    return min;
}

Then you can call it like

public static void main(String[] args) {
    String[] names = { "bob", "maxwell", "charley", "tomtomjack" };
    int longString = getLongString(names);
    System.out.println("The Longest String is: " + names[longString] + " With The Index Of " + longString);

    int shortString = getShortString(names);
    System.out.println("The Longest String is: " + names[shortString] + " With The Index Of " + shortString);
}

Which outputs

The Longest String is: tomtomjack With The Index Of 3
The Longest String is: bob With The Index Of 0

Upvotes: 3

Related Questions