Search code examples
javafortranwhere-clauseequivalent

Equivalent of 'where' fortran keyword in Java?


I'm writing a Java program in which I'm checking a list against a string, and then doing stuff to that. In fortran I'd write something along the lines of

where(list(:)==stringToCheck){
    ...
    statements
    ...
    }

Instead I have a headache of a block of for-loops, if staments and breaks all over the place. No perhaps I could neaten the code a little but it still feels far more inefficient than fortran.

Edit, this is the code I've resorted to:

for(int idx=0;idx<player.get_charactersOwned().size();idx++)
            {
                if(player.get_charactersOwned().get(idx).get_characterName().equals(charName))
                {
                    /* Add character to the game
                     * Add game to the character*/
                    System.out.println("Character "+charName+" Found ");
                    gameToMake.addCharacters(player.get_charactersOwned().get(idx));
                    player.get_charactersOwned().get(idx).addGame(gameToMake);
                    break;
                }else 
                {
                    System.err.println("Character "+ charName +" not found");
                    System.out.println("Shall I add that Character? y/n ");
                    choice = scanner.nextLine();
                    if(choice.equalsIgnoreCase("y"))
                    {
                        charName = scanner.nextLine();
                        Character character = new Character(charName);
                        characterTempList.add(character);
                        player.addCharacter(characterTempList);
                        gameToMake.addCharacters(player.get_charactersOwned().get(idx));
                        player.get_charactersOwned().get(idx).addGame(gameToMake);
                        break;
                    }else{break;}
                }
            }

As tempting as it is to fix this code, I'd much rather use a work around.

Is there a Java equivilant of this without the use of external libraries?


Solution

  • No, there isn't an equivalent in Java. Instead if you need to check if a list of characters (each with a name) contains a character name then simply do this:

    // search the name
    boolean found = false;
    for (Character c : player.get_charactersOwned()) {
        if (c.get_characterName().equals(charName)) {
            found = true;
            break;
        }
    }
    
    // perform the check
    if (found) {
        // do something
    } else {
        // do something else
    }
    

    And by the way, Character is a bad name for your class, it clashes with Java's own Character class. Rename it if possible, to avoid confusion. Alternatively, the loop could have been written like this:

    boolean found = false;
    for (int i = 0, n = player.get_charactersOwned().size(); i < n && !found; i++) {
        Character c = player.get_charactersOwned().get(i);
        if (c.get_characterName().equals(charName)) {
            found = true;
        }
    }