Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way to perhaps do it: Create a Element object and a Pokémon object (perhaps as a subclass?) That holds the necessary information. You could also create Region object. In order to avoid so many if/else/switch statements, you could place everything into arrays/array list/maps. You can simply call some sort of method to return the appropriate pokemon/element/region. I believe this will clean the code up a lot. </p> <p>I was able to simplify this a lot just by adding the <strong>Pokemon</strong> class. Granted there are still a lot of things I would change with this code personally, I think the following is a good start for you.</p> <pre><code>import javax.swing.*; import java.util.ArrayList; public class StarterPokedex { public static void main(String[] args) { ArrayList&lt;Pokemon&gt; p = new ArrayList&lt;Pokemon&gt;(); String choice, pokemon = "", searchBy, region, type, list = "", str = ""; p.add(new Pokemon("Bulbasaur", "Kanto", 1, "Seed", "Grass", "Grassland", "A strange seed was planted on its back at birth." +" The plant sprouts and grows with this Pokémon.")); p.add(new Pokemon("Charmander", "Johto", 2, "Seed", "Fire", "Grassland", "A strange seed was planted on its back at birth." +" The plant sprouts and grows with this Pokémon.")); p.add(new Pokemon("Squirtle", "Kanto", 3, "Seed", "Water", "Grassland", "A strange seed was planted on its back at birth." +" The plant sprouts and grows with this Pokémon.")); choice = JOptionPane.showInputDialog(null, "Do you know the name of the Starter Pokemon you are looking for?" + "(Yes/No)", "Welcome to the Starter Pokedex!", 3); if (choice.equalsIgnoreCase("yes")) { pokemon = JOptionPane.showInputDialog(null, "Enter the name of the Starter Pokemon: ", "Welcome to the Starter Pokedex!", JOptionPane.PLAIN_MESSAGE); } else if (choice.equalsIgnoreCase("no")) { searchBy = JOptionPane.showInputDialog(null, "Search by: 'type' or 'region'?", "Welcome to the Starter Pokedex!", 3); if (searchBy.equalsIgnoreCase("type")) { type = JOptionPane.showInputDialog(null, "Enter pokemon type: (Fire/Water/Grass)", "Welcome to the Starter Pokedex!", JOptionPane.PLAIN_MESSAGE); for (int i = 0; i &lt; p.size(); i++) { if (p.get(i).getType().equals(type)) { list = list + p.get(i).getName() + "\n"; } } pokemon = JOptionPane.showInputDialog(null, list, JOptionPane.PLAIN_MESSAGE); } else if (searchBy.equalsIgnoreCase("region")) { region = JOptionPane.showInputDialog(null, "Enter region: (Kanto/Johto/Hoenn/Sinnoh/Unova/Kalos)", "Welcome to the Starter Pokedex!", JOptionPane.PLAIN_MESSAGE); for (int i = 0; i &lt; p.size(); i++) { if (p.get(i).getRegion().equals(region)) { list = list + p.get(i).getName() + "\n"; } } pokemon = JOptionPane.showInputDialog(null, list, JOptionPane.PLAIN_MESSAGE); } } for (int i = 0; i &lt; p.size(); i++) { if (p.get(i).getName().equals(pokemon)) { str = p.get(i).toString(); } } JOptionPane.showMessageDialog(null, str, "Pokedex Results", 1); } } </code></pre> <p>And here is the pokemon class.</p> <pre><code>public class Pokemon { private String name; private String region; private int pokeNumber; private String species; private String type; private String habitat; private String description; public Pokemon(String name, String region, int pokeNumber, String species, String type, String habitat, String description) { this.name = name; this.region = region; this.pokeNumber = pokeNumber; this.species = species; this.type = type; this.habitat = habitat; this.description = description; } public String getName() { return name; } public String getRegion() { return region; } public int pokeNumber() { return pokeNumber; } public String getSpecies() { return species; } public String getType() { return type; } public String getHabitat() { return habitat; } public String getDescription() { return description; } public String toString() { return "Pokemon: " + name + "\nRegion: " + region + "\nNational Pokemon #: " + pokeNumber + "\nSpecies: " + species + "\nType: " + type + "\nHabitat: " + habitat + "\nDescription: " + description; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload