Quantcast
Channel: Active questions tagged filereader - Stack Overflow
Viewing all articles
Browse latest Browse all 107

In Java, how do I check if a specific element appears in an array? [duplicate]

$
0
0

Please bear with me, I am very new to Java. I have created an array that is created from two text files using File Reader when the program is run, I need to check if an element in the list is a specific word. The array is characterData, which comes from text files that have names and houses. I need to return the number of times each house appears in the array, but I don't know how to write that into my loop. Do I need to change how the array is created so house is on its own? Or is there a way to check it in the current array? I've included how the array is created and the loop. With the way it's done, isn't one element "name,house", which wouldn't be able to just equal house?

Creating the array:

private Character[] characterData;  //create character object with the text files  public DataScene(String nameFile, String houseFile){    characterData = createCharacter(nameFile, houseFile);  }//file reader to convert the files into stringspublic Character[] createCharacter(String nameFile, String houseFile) {    String[] nameData = FileReader.toStringArray(nameFile);    String[] houseData = FileReader.toStringArray(houseFile);    Character[] tempCharacter = new Character[nameData.length];    for (int index = 0; index < tempCharacter.length; index++) {      tempCharacter[index] = new Character(nameData[index], houseData[index]);    }    return tempCharacter;  }

The method to check the houses:

  public int countHouses(String characterData){    int countG = 0;    int countS = 0;    int countH = 0;    int countR = 0;    for (int index = 0; index < characterData.length; index++){      if (house == "Gryfinndor"){        countG++;        return countG;      } else if (house == "Slytherin"){        countS++;        return countS;      } else if (house == "Hufflepuff"){        countH++        return countH;      } else {        countR++;        return countF;      }    }  }

Viewing all articles
Browse latest Browse all 107

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>