0

Surely the solution to this is the following:

public long myFunc(String name) throws Exception {
    for(int i=0;i<amount;i++){ 
       if(this.otherString[i].equals(name)) 
           return longArray[i]; 
    } 
    throw new Exception("Not found"); 
} 

However, this does not seem to be the case.

12
  • 2
    Why does it not seem to be the case? Commented Nov 30, 2015 at 20:10
  • Its gets to the //string not found area Commented Nov 30, 2015 at 20:12
  • This code looks 100% correct. The truth is somewhere else. Case sensitivity maybe? Commented Nov 30, 2015 at 20:12
  • 1
    If you can produce a short but complete program demonstrating the problem, we can help work out what is going on. Until that point, unfortunately, we really can't. Commented Nov 30, 2015 at 20:14
  • 1
    What is amount? Try using otherString.length to make sure you go through the whole array Commented Nov 30, 2015 at 20:29

4 Answers 4

4

You can use Guava, then your code can looks like this

String[] stringArray = {"s1", "s2", "s3"};

int index = Iterators.indexOf(Iterators.forArray(stringArray), new Predicate<String>() {
    @Override
    public boolean apply(String input) {
        return input.equals("s2");
    }
});

or simpler

int index = Arrays.asList(stringArray).indexOf("s2");

Your code can also look like this

public class Finder {

    private String[] stringArray = {"s1", "s2", "s3"};

    public int findIndex(String name) {
        for (int i = 0; i < stringArray.length; i++) {
            if (stringArray[i].equals(name))
                return i;
        }

        throw new RuntimeException("Not found");
    }

    public static void main(String... s) {
        int index = new Finder().findIndex("s1");

        System.out.println(index);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I have updated the original post. Although, your method works, that's not really the point. It's more a matter of why doesnt the my method work
@MatthewFrost I see that your post has changed a little bit. So I modify a little bit my answer. I instead of amount uses this.otherString.length and instead of return longArray[i] uses return i.
What does the "or simpler" return if it cant find "s2"? (-1 sorry )
@MatthewFrost Return -1. In JavaDoc you can read Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element..
Thank you. But this still doesn't work for my code. I'll have to look further.
|
0

You can either run your code through a debugger and find out why it doesn't work, or add some println traces to your original code and you'll see what the problem is:

public long myFunc(String name) throws Exception {
    System.out.println("Looking for: " + name);
    for (int i = 0; i < amount; i++){
        if(this.otherString[i].equals(name))
            return longArray[i];
        System.out.printf("%4d: \"%s\": No match.%n", i, this.otherString[i]);
    }
    for (int i = amount; i < this.otherString.length; i++)
        System.out.printf("%4d: \"%s\": Not checked.%n", i, this.otherString[i]);
    throw new Exception("Not found");
}

By the way, make sure you are correctly interpreting the method's behaviour. Could it be that it's finding it but throwing an ArrayIndexOutOfBoundsException because i is greater than longArray.length, and you misinterpret that as the exception you are explicitly throwing?

4 Comments

And what results did you get when you did this?
As expected they all say no match/not checked
So are you saying there is a string between otherString[0] and otherString[amount-1] that equals 'name', and yet the method still prints "No match"?
As strange as it sounds yes. I have gone through it with a debugger, and the strings do match at one point, but the code within the if statement never executes.
0

Turns out '\u0000' was at the end of the string that was meant to be equal. This does not show in printing. Next time I will be more ruthless in the inspection of the debugging. Thank you for all the suggestions though, and sorry for wasting your time.

Comments

-2

This might be completely wrong, but isn't the problem just missing curly brackets in the if statement? I am new to the java language and the example is probably messy, but it uses the same structure from your question and works just fine:

public class random_class {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] a = new String[] {"hi!", "hello world!", "ho ho ho world!"};       
    int b = getHWIndex(a);
    System.out.println(b);
}

public static int getHWIndex(String[] stringArray){
    int i;
    Boolean test = false;
    for(i=0;i<stringArray.length;i++){
        if(stringArray[i].equals("hello world!")){
            test = true;  
            break;
        }
    }
    if(test == true){
    return i;}else{
        return i = 0; // this is not a good answer... 
//but as you return an int I could not think on a quick way to fix   the return when there is no match.
        }
    }
}

1 Comment

No Java supports 1 line with no curly braces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.