1

TERADENWOORDEN is the array with [0][0] = book and [0][1] = lord of the rings

CATEGORIES = amount of categories

geefNamenCat is a method that returns an input.readString() to gekozenCat

am I doing something wrong here?

public void catKiezen() {
   boolean bevatGekozen = false;
   String gekozenCat
    do {
        gekozenCat = geefNamenCat();
        for (String[] TeradenwoordenLijst : Arrays.asList(TERADENWOORDEN))
            if (Arrays.asList(TeradenwoordenLijst).contains(gekozenCat)) {
                bevatGekozen = true;
                break;
            }
        if (! bevatGekozen) {
            System.out.println("De opgegeven categorie is ongeldig.");
        }
    } while (! bevatGekozen);
   String woordVanCat = woordBepalen (gekozenCat);
}

public String woordBepalen(String teBepalenWoordVanCat) {
    for (int p = 0; p < CATEGORIES; p++)
       if (TERADENWOORDEN[p][0].equals(teBepalenWoordVanCat))
       return TERADENWOORDEN[p][1];
}

in the end i get an error cannot find symbol for the line String woordVanCat = woordBepalen (gekozenCat);

update:

I am now getting a 'missing return statement'

public String woordBepalen(String teBepalenWoordVanCat) {
    for (int p = 0; p < CATEGORIES; p++) {
        if (TERADENWOORDEN[p][0].equals(teBepalenWoordVanCat)) {
        return TERADENWOORDEN[p][1];
        }
    }
}
0

3 Answers 3

5

you have declared String gekozenCat = geefNamenCat(); inside your do-while and trying to access it outside the loop.gekozenCat is only confined with-in your do-while. declared it outside do-while if you want to access it outside your do-while.

Sign up to request clarification or add additional context in comments.

4 Comments

Why don't you try to compile it?
Alright so that did indeed do the trick, I feel dumb now. Anyway it gave me a new error. Missing return statement. I guess this is because the return is inside the loop too? I have updated the original post
@Jente absolutely, check gefei answer. he mentioned it in his answer :)
Thanks a lot everyone. Was superfast too! :)
2

as far as I can see your method woordBepalen does not compile, because the compiler is not sure that it has a well-defined value to return

public String woordBepalen(String teBepalenWoordVanCat) {
    for (int p = 0; p < CATEGORIES; p++)
       if (TERADENWOORDEN[p][0].equals(teBepalenWoordVanCat))
       return TERADENWOORDEN[p][1];
}

EDIT

You'll have to define what do return in case for no p TERADENWOORDEN[p][0].equals(teBepalenWoordVanCat) holds. Like this:

 public String woordBepalen(String teBepalenWoordVanCat) {
        for (int p = 0; p < CATEGORIES; p++) {
           if (TERADENWOORDEN[p][0].equals(teBepalenWoordVanCat))
             return TERADENWOORDEN[p][1];
        }
        return null;
 }

It's up to you to decide if returning null is the right thing to do. Another possibility is throwing an exception

2 Comments

Sjuan76 predicting the future. Some simple fix for this too then I guess?
Awesome! Thank you very much guys. I would upvote if I could but i need 4 more rep ;p
1

The scope of gekozenCat is inside the do..while block. Outside that block, you have not defined it so that causes the error.

In general, if you have a block { }, variables defined inside that block will only be available inside that block (and after they have been defined).

Comments

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.