dimanche 25 février 2018

Printing User Input/Multidimensional Array

I apologize if this is a repeat question. I'm not sure what to look for.

This is for an assignment so I would really like a push in the right direction versus just getting the answer. The code is to repeatedly ask a user to enter a state name which returns the state bird and state flower. Once the user has entered "None," a summary prints out that contains the state, bird, and flower of each state the user entered. Currently, the summary only prints the information of the LAST state entered by the user.

I am sorry for the sloppy work. I'm only beginning to learn!

public class StateInformation {

    private String[][] stateInfo =  {
        {"Alabama", "Yellowhammer", "Camellia"},
        {"Alaska", "Willow Ptarmigan", "Alpine Forget-Me-Not"},
        {"Arizona" , "Cactus Wren", "Saguaro Cactus Blossom"},
        {"Arkansas", "Northern Mockingbird", "Apple Blossom"},
        {"California", "California Quail", "California Poppy"},
        {"Colorado", "Lark Bunting", "Rocky Mountain Columbine"},
        {"Connecticut", "American Robin", "Mountain Laurel"},
        {"Delaware", "Blue Hen Chicken", "Peach Blossom"},
        {"Florida", "Northern Mockingbird", "Orange Blossom"},
        {"Georgia", "Brown Thrasher", "Cherokee Rose"},
        {"Hawaii", "Nene", "{Pua Aloalo"},
        {"Idaho", "Mountain Bluebird", "Syringa"},
        {"Illinois", "Greater Prairie-Chicken", "Violet"},
        {"Indiana", "Northern Cardinal", "Peony"},
        {"Iowa", "Eastern Goldfinch", "Wild Rose"},
        {"Kansas", "Weatern Meadowlark", "Wild Native Sunflower"},
        {"Kentucky", "Northern Cardinal", "Goldenrod"},
        {"Louisana", "Brown Pelican", "Louisana Iris"},
        {"Maine", "Black-Capped Chickadee", "White Pine Cone and Tassel"},
        {"Maryland", "Baltimore Oriole", "Black-Eyed Susan"},
        {"Massachusetts", "Black-Capped Chickadee", "Mayflower"},
        {"Michigan", "American Robin", "Apple Blossom"},
        {"Minnesota", "Common Loon", "Pink and White Lady Slipper"},
        {"Mississippi", "Northern Mockingbird", "Magnolia"},
        {"Missouri", "Eastern Bluebird", "White Hawthorn Blossom"},
        {"Montana", "Western Meadowlark", "Bitterroot"},
        {"Nebraska", "Western Meadowlark", "Goldenrod"},
        {"Neveda", "Mountain Bluebird", "Sagebrush"},
        {"New Hampshire", "Purple Finch", "Pink Lady's Slipper"},
        {"New Jersey", "Eastern Goldfinch", "Violet"},
        {"New Mexico", "Greater Roadrunner", "Yucca"},
        {"New York", "Eastern Bluebird", "Rose"},
        {"North Carolina", "Northern Cardinal", "Dogwood"},
        {"North Dakota", "Western Meadowlark", "Wild Prairie Rose"},
        {"Ohio", "Northern Cardinal", "White Trillium"},
        {"Oklahoma", "Scissor-Tailed Flycatcher", "Mistletoe"},
        {"Oregon", "Western Meadowlark", "Oregon Grape"},
        {"Pennslyvania", "Ruffed Grouse", "Mountain Laurel"},
        {"Rhode Island", "Rhode Island Red Chicken", "Violet"},
        {"South Carolina", "Carolina Wren", "Yellow Jessamine"},
        {"South Dakota", "Ring-necked Pheasant", "American Pasque"},
        {"Tennessee", "Northern Mockingbird", "Passion Flower"},
        {"Texas", "Northern Mockingbird", "Ennis"},
        {"Utah", "California Gull", "Sego Lily"},
        {"Vermont", "Hermit Thrush", "Red Clover"},
        {"Virginia", "Northern Cardinal", "American Dogwood"},
        {"Washington", "Willow Goldfinch", "Coast Rhododendron"},
        {"West Virginia", "Northern Cardinal", "Rhododendron"},
        {"Wisconsin", "American Robin", "Wood Violet"},
        {"Wyoming", "Western Meadowlark", "Indian Paintbrush"},
    };//End array initialization
public StateInformation() {
}
public String[][] getStateInfo(){
   return stateInfo;
}
public void setState(String[][] state) {
    this.stateInfo = stateInfo;
}
}//End of Class

Here is the second class:

//Second Class
import java.util.Scanner;

public class TestStateInformation {
public static void main(String [] args) {

StateInformation states = new StateInformation();   
String [][] state = states.getStateInfo();  
//Inserting scanner & variable. 
Scanner scanner = new Scanner(System.in);

while(true) {
    //Prompt the user to input a state name.  
    System.out.println("Please enter a state or None to exit: ");
    //Read the state entered by user, including leading/trailing white spaces.
    String stateInfo = scanner.nextLine();

    //If loop to end if None is entered.
    if (stateInfo.equalsIgnoreCase("None")) {
        break;
    } else {
        int position = getStatePosition(state, stateInfo);
        if (position != -1) {
            System.out.println("Bird: " + getBird(position, state) +'\n' + "Flower: " + getFlower(position, state) + '\n');
        }
        if ((scanner.nextLine().equals("None"))) {
                System.out.println("***** Thank you! *****" + '\n' + "A summary report for each State, Bird, and Flower is: "
                         + '\n' + getState(position, state)+ ", "+ getBird(position, state) + ", "+ getFlower(position, state)
                         + '\n' + "Please visit our site again!");
            }//end if loop for printing bird and flower of state entered by user.
        }//End else loop
    }//end if loop
}//end while loop
private static int getStatePosition(String state[][], String stateInfo) {
    for (int i = 0; i < state.length; i++) {
        if (stateInfo.equalsIgnoreCase(state[i][0])) {
            return i;
        }
    }
return -1;
}
//Creates the position of the state name into [0]
private static String getState(int position, String [][] state) {
    return state[position][0];  
}
    //Creates the position of the state bird into [1].
    private static String getBird(int position, String [][] state) {
        return state[position][1];
}
    //Creates the position of the state bird into [2]
    private static String getFlower(int position, String [][] state) {
        return state[position][2];
}
}

Aucun commentaire:

Enregistrer un commentaire