Question
I have one last issue with the method summaryout: just cant get it right: StateFlowerBird.java:130: error: incompatible types: Object cannot be converted to String for
I have one last issue with the method summaryout: just cant get it right:
StateFlowerBird.java:130: error: incompatible types: Object cannot be converted to String
for (String userState : inputStates) {
^
1 error
/*
* File: USStateFlowerBird.java
* Author: Mike Duncan
* Date: Aprl 7, 2020
* Purpose: User enter a State name without worrying about case
* and it displays the State bird and flower.
*/
import java.util.Scanner; //import Scanner so you can type in the state name.
import java.util.Arrays; // to print arrays in Java.
import java.util.ArrayList;
public class StateFlowerBird {
//2D Array with State, state flower, and state bird from:
//https://en.wikipedia.org/wiki/Talk%3AList_of_U.S._states_and_their_state_flower,_tree,_and_bird/Archive
static String[][] stateInfo = {
{"Alabama", "Camellia", "Yellowhammer"},
{"Alaska", "Forget-me-not", "Willow Ptarmigan"},
{"Arizona", "Saguaro cactus", "Cactus Wren"},
{"Arkansas", "Apple blossom", "Mockingbird"},
{"California", "Golden poppy", "California Valley Quail"},
{"Colorado", "Rocky Mountain Columbine", "Lark Bunting"},
{"Connecticut", "Mountain laurel", "Robin"},
{"Delaware", "Peach blossom", "Blue Hen Chicken"},
{"Florida", "Orange blossom", "Mockingbird"},
{"Georgia", "Cherokee rose", "Brown Thrasher"},
{"Hawaii", "Hibiscus", "Nene"},
{"Idaho", "Syringa", "Mountain Bluebird"},
{"Illinois", "Native violet", "Cardinal"},
{"Indiana", "Peony", "Cardinal"},
{"Iowa", "Wild rose", "Eastern Goldfinch"},
{"Kansas", "Native sunflower", "Western Meadowlark"},
{"Kentucky", "Goldenrod", "Cardinal"},
{"Louisiana", "Magnolia", "Eastern Brown"},
{"Maine", "Pine cone & tassle", "Chickadee"},
{"Maryland", "Black Eyed Susan", "Baltimore Oriole"},
{"Massachusettes", "Mayflower", "Chickadee"},
{"Michigan", "Apple blossom", "Robin"},
{"Minnesota", "Lady slipper", "Common Loon"},
{"Mississippi", "Magnolia", "Mockingbird"},
{"Missouri", "Hawthorn", "Bluebird"},
{"Montana", "Bitterroot", "Western Meadowlark"},
{"Nebraska", "Goldenrod", "Western Meadowlark"},
{"Nevada", "Sagebrush", "Mountain Bluebird"},
{"New Hampshire", "Purple lilac", "Purple Finch"},
{"New Jersey", "Purple violet", "Eastern Goldfinch"},
{"New Mexico", "yucca", "Roadrunner"},
{"New York", "Rose", "Bluebird"},
{"North Carolina", "Dogwood", "Cardinal"},
{"North Dakota", "Wild prairie rose", "Western Meadowlark"},
{"Ohio", "Scarlet carnation", "Cardinal"},
{"Oklahoma", "Mistletoe", "Scissor-tailed"},
{"Oregon", "Oregon Grape", "Western Meadowlark"},
{"Pennsylvania", "Mountain laurel", "Ruffed Grouse"},
{"Rhode Island", "Violet", "Rhode Island Red"},
{"South Carolina", "Yellow jessamine", "Great Carolina Wren"},
{"South Dakota", "Pasque flower", "Ring-necked Pheasant"},
{"Tennesssee", "Purple iris", "Mockingbird"},
{"Texas", "Texas Blue Bonnet", "Mockingbird"},
{"Utah", "Sego lily", "American Seagull"},
{"Vermont", "Red clover", "Hermit Thrush"},
{"Virginia", "Dogwood", "Cardinal"},
{"Washington", "Western rhododendron", "Willow Goldfinch"},
{"West Virginia", "Rhododendron", "Cardinal"},
{"Wisconsin", "Wood violet", "Robin"},
{"Wyoming", "Indian paint brush", "Western Meadowlark"}};
public static void main(String[] args) {
Scanner scannerIn = new Scanner(System.in); //Scanner
String state = "";
findState();
}
private static void findState() {
Scanner scannerIn = new Scanner(System.in);
String state = "";
int index=0;
String[] summary;
summary = new String[50];
ArrayList
do { //do loop
//Ask user to type the name of state or none
System.out.print(" Enter a State or None to exit: ");
state = scannerIn.nextLine();
boolean found = false; //used to indicate the desired state is found
String output = "";
for (int i = 0; i < 50; i++) { //loop that goes from i=0 to <50 (50 states in the 2D array)
if (stateInfo[i][0].equalsIgnoreCase(state)) {
index=i;
found=true; //State info found
} else if (!stateInfo[i][0].equalsIgnoreCase(state)) { //to check if user input (String) equals stateBirdFlower [i][0]
}
} //for loop
if (found) { // IF true, then save state + bird + flower as a String (say output). Use concatenation operator (+) to append state, bird, flower.
output = "Flower: " + stateInfo[index][1] + " Bird: " + stateInfo[index][2]; // IF found true, then save state + bird + flower as a String
// stateInfo [i][0] stores state; stateInfo [i][1] & stateInfo [i][2] stores flower & bird details;
summary[index] = output;// Copy the String (output) to a 1-D array as: summary[counter] = output;
inputStates.add( stateInfo[index][0] + ", " + stateInfo[index][1] + ", " + stateInfo[index][2]);
output(index, summary); // method that outputs the contents of summary[] array
} // if (found)
} //do loop
while (!state.equalsIgnoreCase("None"));
if (state.equalsIgnoreCase("None")) { //Look for input of None to exit
System.out.println(" **** Thank you ***** " + "A summary report for each State, Bird, and Flower is: ");
summaryout(index, inputStates);
// for (String userState : inputStates) {
//System.out.println(userState);
//}
System.out.println("Please visit our site again! ");
}
System.exit(0);
}
public static int output(int i, String[] summary) {
System.out.print(summary[i] + " ");
return i;
}
public static int summaryout(int i, String[] inputStates) {
for (String userState : inputStates) {
System.out.println(userState);
}
return i;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started