-1

So I have a method called indexOf and by specifying a string in main, I can make the program print out the index of that string within the array. But how would I go about simply having a method that can print out the first index of an array, without me needing to specify a string, at all? If the index does not exist, I want it to return -1.

public static void main(String[] args)
{

    String[] v = new String[1];
    v[0] = "test";

    String s = "t";

    indexOf(v, s);
}

public static int indexOf(String[] v, String s)
{       
    int i = v[0].indexOf(s);
    System.out.println
        ("Index of the first string in the first array: " + i);

    return -1;                                      
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
user3071133
  • 55
  • 1
  • 4
  • 7
    It's not clear what you're trying to do. The first index of an array will always be `0` (or undefined if the array has no elements). – David Jan 27 '14 at 15:46
  • Right now, I am going to get 0 printed out because String s = "t"; is the first string and it has the index 0. What I want, is for the program to actually print out "t" and basically recognize the first index and let me know what it is. – user3071133 Jan 27 '14 at 15:49
  • You never loop over the input array. You also never check the bounds of the input array. You do nothing with the return value of the function. – crush Jan 27 '14 at 15:52
  • It's still really unclear what you're asking. Do you just want to print the first element in the array? Again, that element will always be at index 0, or undefined for an empty array. – David Jan 27 '14 at 15:52
  • @David I assume that he wants to use String.indexOf() on an array of String's, and return the first occurrence. What's unclear is how he plans on knowing to which element of the array the index belongs without returning either an array of `int` or some custom result type. – crush Jan 27 '14 at 15:54

2 Answers2

0

The first index of an array is always 0. So you would just return v.length > 0 ? 0 : -1.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
0

Your question can be read two ways. If you want to be able to return just the first index of the array, then do the following:

if(v.length > 0)//check length
    return 0;//return first position
return -1;//return empty string meaning there wasnt a first position.

However, you may mean to ask to return the first case of a String s in the array v. Then in that case, do the following:

//assuming v and s are not null or do not contain null values
for(int i = 0; i < v.length; i++){//loop through the array v
    if(v[i].equals(s){//does the current value of v equal to the String s?
        return i;//found string!
    }
}
return -1;//didnt find the string

You seem new to java. I highly recommend reading these two sources:

Java: Array with loop

How do I compare strings in Java?

Community
  • 1
  • 1
  • I was infact a bit confused but this answer helped me understand it. I was going for the first answer you gave. Essentially the index will always be 0 as long as the string exists, otherwise I wished to return -1. – user3071133 Jan 27 '14 at 16:13