1

I'm trying to check if a string is part of a 2dim array.

geefNamenCat returns Input.readString(). Is there any way for me to easily check if geefNamenCat is part of TERADENWOORDEN[ ][ ]?. I've been browsing this site for quite a while now and I have only found solutions for 1dim arrays (which I couldn't get to work either).

    public void catKiezen() {
        do { 
        String gekozenCat = geefNamenCat("\nKies 1 van de volgende categorieën: ");
        }
        while ( !gekozenCat.equals (TERADENWOORDEN[][]) 
    }

Edit: I forgot to add, the do/while thing is so that it would keep asking for a new input until the given input is part of the array

Baldrick
  • 23,882
  • 6
  • 74
  • 79
Jente
  • 63
  • 1
  • 8
  • Part - in which direction? When read as text? Right or down? Left, right, up or down? Any of the eight directions? Is it a rectangle? A torus? ... The hungarian (or what it is) doesn't help understanding. – John Dvorak Jan 05 '13 at 14:41
  • Or is it a 2D array of strings, and the input must be present in any cell? I have no idea what you're trying to do. – John Dvorak Jan 05 '13 at 14:45
  • The array reads lines from a txt file. Basicly you have 2 columns and x amount of rows, where x = amount of lines in the txtfile. Thanks – Jente Jan 05 '13 at 14:45
  • Your second comment is correct, sorry if I didn't explain well enough. It needs to check if the input string is part of the array (which is a String array). preferably it should only check the first column, but that's not really an issue. – Jente Jan 05 '13 at 14:45
  • `TERA...DEN` is the 2d array? – John Dvorak Jan 05 '13 at 14:46
  • Is the size of the 2D array defined/specified? – Priyanka.Patil Jan 05 '13 at 14:47
  • Checking one column is _slightly_ easier than checking two columns. – John Dvorak Jan 05 '13 at 14:47

2 Answers2

1

You could try this:

boolean contains = false;

do {
    String gekozenCat = geefNamenCat("\nKies 1 van de volgende categorieën: ");

    for (String[] sub : Arrays.asList(TERADENWOORDEN))
        if (Arrays.asList(sub).contains(gekozenCat)) {
            contains = true;
            break;
        }

    if (! contains) {
        // print something
    }

} while (! contains);
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • No need to cast to a List; also, you might be facing unchecked casts (Object => String[][]) (error) this way. – John Dvorak Jan 05 '13 at 14:48
  • @JanDvorak There is no cast here. – arshajii Jan 05 '13 at 14:49
  • @JanDvorak The question is: would the quick containment test of the `HashSet` outweigh the time it takes to construct it. I figured it would but I could definitely be wrong. Also, what unchecked cast are you talking about? – arshajii Jan 05 '13 at 14:54
  • You have to import `java.util.HashSet` and `java.util.Arrays`. Also, you should put `boolean contains = false` outside the loop and the rest of the code inside. Then the loop condition would be `! contains`. – arshajii Jan 05 '13 at 14:57
  • @A.R.S. since you're building a new hashSet each time... I assume that it does take much longer. – John Dvorak Jan 05 '13 at 14:58
  • ah sorry, forgot HashSet, my bad – Jente Jan 05 '13 at 14:58
  • Still getting errors on contains(gekozenCat) (cannot find symbol) – Jente Jan 05 '13 at 15:00
  • Note that this fails the requirement that `preferably it should only check the first column`. – John Dvorak Jan 05 '13 at 15:05
  • 1
    This actually works. nice one. Just one more minor thing. Say I wanted to display a message after typing something in the input that does not belong to the array, where do I put this? just add else to the if? Going to upvote this too. Thanks a lot! – Jente Jan 05 '13 at 15:08
  • That's right Jan, if you could come up with something that does, that would be great. But it's not really needed – Jente Jan 05 '13 at 15:09
  • @Jente mine does, and it's shorter as well. – John Dvorak Jan 05 '13 at 15:10
  • @Jente You could just put an `if (! contains) {...}` at the end of the loop body. See my edit. – arshajii Jan 05 '13 at 15:10
  • Sry I had to go out for a bit. Let me try yours now then :). This one certainly works though if someone has the same problem. – Jente Jan 05 '13 at 15:50
0

Based on the comments:

Basicly you have 2 columns and x amount of rows

preferably it should only check the first column

Perhaps this is what you want?

for(String[] row: ALLOWED_WORDS){
  if(input.equals(row[0])){
    found = true;
    break;
  }
}

Also, if you expect multiple queries over the same table, you might consider compiling the table into a hashSet:

//once
HashSet<String> allowedWordList = new HashSet<String>();
for(String[] row: ALLOWED_WORDS){
  allowedWordList.add(row[0])
}

//on each check
if(allowedWordList.contains(input)){
  ...
}

Also, based on

The array reads lines from a txt file

Then it's not a constant and should not be all-caps: ALLOWED_WORDS => allowedWords

Community
  • 1
  • 1
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
  • It actually reads it from a text file on startup into a final so I guess I should be going with the all caps one. Let me try this – Jente Jan 05 '13 at 15:00
  • All-caps normally signify compile-time constants only. However, I do understand your thinking. – John Dvorak Jan 05 '13 at 15:02
  • I can't get this to work properly, probably something on my end though. input should be geefNamenCat and allowedWordList should be TERADENWOORDEN for me, correct? – Jente Jan 05 '13 at 16:00
  • @Jente I think so. Note that my code doesn't include the outer loop (reading in repeatedly). – John Dvorak Jan 05 '13 at 16:01