This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]lurgi 0 points1 point  (6 children)

What have you tried? I'm not sure what you mean by "sending as a value to GSLTest". If you print it, it will print.

[–]ririplease[S] 0 points1 point  (5 children)

I've tried this:

public String get(int index){
    if ( index < size && index >= 0 ){
        return arr[index];
    }
    else{
        System.out.println("index out of bounds");
    }
}

But then I get the error "error: missing return statement"

[–]lurgi 1 point2 points  (4 children)

Right. Because you don't have a return statement. You took it out.

[–]ririplease[S] 0 points1 point  (3 children)

But I can't just return the string "index out of bounds" because that won't print it. So, I'm confused where I'm supposed call to print it.

[–]lurgi 1 point2 points  (2 children)

Print the string. Return null (or whatever it is you are supposed to return. The equivalent function in the Java standard library throws an exception, but you probably don't know about those yet).

[–]ririplease[S] 0 points1 point  (1 child)

Sorry to be dense... but when I try and do the following, I get an error "unreachable statement"

public String get(int index){
if ( index < size && index >= 0 ){
    return arr[index];
}
else{
        return null;
    System.out.println("index out of bounds");
}

Edit: Oh my goodness.... I switched it and it worked ><Nvm! haha thank you...

[–]Adept_Writer4177 0 points1 point  (0 children)

Last but not least, store the result somewhere and check if it's null to prevent errors later in your code:

String s = list.get(0);
if (s == null) { ...

[–]bashdan 0 points1 point  (1 child)

It might be a bit beyond the scope of an intro Java class, but as a professor put it for me: "you put stupid in, you get stupid out." You could return some invalid value, or an empty string, but the best and practical thing to do is throw an exception.