HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

50 states info program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
statesinfoprogram

Problem

Instructions:


Write a Java program the displays the State bird and flower. You should use your IDE for this exercise. The program should prompt the user to enter a state and print both the state bird and flower. The user should be able to enter a State without worrying about case. (e.g. Users could enter Maryland, maryland, MARYLAND or any other possible combination of lower and upper case characters. States may also contain leading and trailing white spaces. Hint: Store the state information in a multi-dimensional array. The program should continue to prompt the user to enter a state until None is entered. You will need to do some research to find the state birds and flowers.

```
import java.util.Scanner;

public class StateInfo {

public static int getInfo(String stateInfo[][],String state)
{
int position = -1;
boolean found = false;
for (int index=0; index<stateInfo.length && !found; index++)
{
if(stateInfo[index][0].equalsIgnoreCase(state))
position=index;
}
return position;
}

public static void main(String[] args) {

Scanner userInput = new Scanner(System.in);

String[][] stateInformation = new String[][] {
{"Alabama", "Yellowhammer", "Camelia"},
{"Alaska", "Willow Ptarmigan", "Forget-Me-Not"},
{"Arizona", "Cactus Wren", "Saguaro Cactus Blossom"},
{"Arkansas", "Mockingbird", "Apple Blossom"},
{"California", "California Valley Quail", "Golden Poppy"},
{"Colorado", "Lark Bunting", "Rocky Mountain Columbine"},
{"Connecticut", "Robin", "Mountain Laurel"},
{"Delaware", "Blue Hen Chicken", "Peach Blossom"},
{"Florida", "Mockingbird", "Orange Blossom"},
{"Georgia", "Brown Thrasher", "Cherokee Rose"},
{"Hawaii", "Nene", "Hawaiian Hibiscus"},
{"Idaho", "Mountain Bluebird", "Syringa, mock orange"},

Solution

Instead of using a HashMap as EthanBierlein recommended, a more object oriented way to handle the state information would be to setup a class that stores the state bird and the state flower.

Here is what I mean:

public class State {
    private final String flower;
    private final String bird;

    public State(String flower, String bird) {
        this.flower = flower;
        this.bird = bird;
    }

    private String getFlower() {
        return this.flower;
    }
    private String getBird() {
        return this.bird;
    }
}


Notice how there is no name property? Keep reading to find out why.

Now, the easiest way to go through and find the correct state would be to stick all the states in to an array. However, if you instead use a Map, later, when you are retrieving States from the Map, your code will be faster. I'll go more into that later.

Here is what I mean:

Map states = new HashMap<>();
states.put("Alabama", new State("Yellowhammer", "Camelia");
...


Here comes the easiest part. Now all you have to do is pass the state string given to you through user input to Map.get; no iteration needed.

Here is the code:

if( !(state = states.get(stateName)) ) { // search through the states map by state name
    System.out.println("Invalid State Entered");
    continue; // go back to the top of the loop
}
System.out.printf("Bird: %s%nFlower: %s%n", state.getBird(), state.getFlower());


By using a map and then using the Map.get method, you are speeding up your code because this operation runs in O(1) time, rather than iterating through an array of States and checking a name property.

Map recommendation and speed information provided by Simon André Forsberg.

Code Snippets

public class State {
    private final String flower;
    private final String bird;

    public State(String flower, String bird) {
        this.flower = flower;
        this.bird = bird;
    }

    private String getFlower() {
        return this.flower;
    }
    private String getBird() {
        return this.bird;
    }
}
Map<String, State> states = new HashMap<>();
states.put("Alabama", new State("Yellowhammer", "Camelia");
...
if( !(state = states.get(stateName)) ) { // search through the states map by state name
    System.out.println("Invalid State Entered");
    continue; // go back to the top of the loop
}
System.out.printf("Bird: %s%nFlower: %s%n", state.getBird(), state.getFlower());

Context

StackExchange Code Review Q#95785, answer score: 15

Revisions (0)

No revisions yet.