1

yes, this is my homework. But i am stuck and have been for days. (horrible teacher, horrible lessons. this teacher will never respond to me or anyone about anything)

I have seen something like this on here before but never seen or understood their answers.

In this program, the user inputs a state, then it outputs the states bird and its flower. after the user types none, it should give them a summery of everything they typed in a row. State, Bird, Flower. I have not a clue how to do this... You do not have to tell me how, that is fine. but could you please guide me, in a non vague way haha.

import java.util.Scanner;

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

    String theState [][] =  {
            {"Alabama", "Northern Flicker","Camelia"},
            {"Alaska", "Willow PTarmigan","ForgetMeNot"},
            {"Arizona", "Cactus Wren","Saguaro Cactus Blossom"},
            {"Arkansas", "Mockingbird","Apple Blossom"},
            {"California", "California Quail","California Blossom"},
            {"Colorado", "Lark Bunting","Rocking Mountain Columbine"},
            {"Connecticut", "American Robin","Mountain Laurel"},
            {"Delaware", "Blue Hen Chicken","Peach Blossom"},
            {"Flordia", "Mockingbird","Orange Blossom"},
            {"Georgia", "Brown Thrasher","Cherokee Rose"},
            {"Hawaii", "Nene","Hibiscus"},
            {"Idaho", "Peregrine Falcon","Mock Orange"},
            {"Illinois", " Northern Cardinal","Purple Violet"},
            {"Indiana", "Northern Cardinal","Peony"},
            {"Iowa", "Eastern Goldrinch","Wild Prairie Rose"},
            {"Kansas", "Western Meadowlark","Sunflower"},
            {"Kentucky", "Northen Cardinal","Golden Rod"},
            {"Louisiana", "Brown Pelican","Magnolia"},
            {"Maine", "Black-capped Chickadee","White Pine Tassel and Cone"},
            {"Maryland", "Baltimore Oriole","Black-Eyes Susan"},
            {"Massachusetts", "Black-Capped Chickadee","Mayflower"},
            {"Michigan", "Robin Redbreast","Apple Blossom"},
            {"Minnesota", "Common Loon","Pink and White ladyslipper"},
            {"Mississippi", "Wood Duck","Magnolia"},
            {"Missouri", "Eastern Bluebird","Hawthorn"},
            {"Montana", "Western Meadowlark","Bitterroot"},
            {"Nebraska", "Western Medowlark","Goldenrod"},
            {"Nevada", "Mountain Bluebird","Sagebush"},
            {"New Hampshire", "Purple Finch","Purple Lilac"},
            {"New Jersey", "Eastern Goldfinch","Violet"},
            {"New Mexico", "Roadrunner","Yucca"},
            {"New York", "Bluebird","Rose"},
            {"North Carolina", "Cardinal","Flowering Dogwood"},
            {"North Dakota", "Western Meadowlark","Wild Prairie Rose"},
            {"Ohio", "Cardinal","Scarlet Carnation"},
            {"Oklahoma", "Scissor-tailed Flycatcher","Mistletoe"},
            {"Oregon", "Western Medowlark","Orange Grape"},
            {"Pennsylvania", "Ruffed Grouse","Mountain Laurel"},
            {"Rhode Island", "Rhode Island Red","Violet"},
            {"South Carolina", "Great Carolina Wren","Yellow Jessamine"},
            {"South Dakota", "Ring-necked Pheasant","Pasque Flower"},
            {"Tennessee", "Mocking Bird","Iris"},
            {"Texas", "Mocking Bird","Texas Bluebonnet"},
            {"Utah", "Common American Gull","Sego Lily"},
            {"Vermont", "Hermit Thrush","Red Clover"},
            {"Virginia", "Cardinal","Flowering Dogwood"},
            {"Washington", "Willow Goldfinch","Coast Rhodoendron"},
            {"West Virginia", "Cardinal","Rhododendron"},
            {"Wisconsin", "Robin","Violet"},
            {"Wyoming", "Western Medowlark","Indian Paintbrush"},

        };




    Scanner input1 = new Scanner(System.in);

    while (true) {
    System.out.println("Enter a State or None to exit: ");
    String states = input1.nextLine();

    int position=getBirdFlower(theState, states);
    if (states.trim().equalsIgnoreCase("None")){
            System.out.println("**** Thank you *****\r\n" + 
                    "A summary report for each State, Bird, and Flower is: " );
            break;
        }
    else {

        if(position!=-1)
        {
            System.out.println("Bird: "+theState[position][1]);
            System.out.println("Flower: "+theState[position][2]);              
        }
        else
            System.out.println("Invalid state entered");

    //  for (int i=0; i<state.length;i)
        //  for (int j=0;j<state[i].length;j)
            //  System.out.println(state[i][j]);

        }

    }



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

        }
9
  • 1
    Sure, I don't mind helping. Since you are trying to learn can you first format your code then add as much comment as you can about what you think each line is doing and what you hope to do. Maybe try to explain how you would do this if you had a stack of cards with the state name, bird, and flower and I give you a state name. Commented Oct 5, 2018 at 22:58
  • Important point is function getBirdFlower() which should be renamed as it doesn't return the bird and flower data but an index into the data. Hints: found is only set once (so it can't change to true) but you do not really need the variable at all; you can place multiple returns in a function (if it makes sense), also inside a for-loop which is then exited at the return. Commented Oct 5, 2018 at 23:09
  • Please specify what the final summary should contain: Data about all known states, of all states entered by the user or only of the last state entered by user? Commented Oct 5, 2018 at 23:13
  • @michael-butscher i have replaced it with findIndexLocation. and i have removed the found boolean :). It kinda makes sense. I will play with it. Oh the summery should state all the states the user entered earlier, with the bird and the flower. Commented Oct 5, 2018 at 23:48
  • @Charles i will asap Commented Oct 5, 2018 at 23:51

2 Answers 2

1

I added some comment to your code for understand clearly. Please read carefully.

import java.util.ArrayList;
import java.util.Scanner;

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

        String theState[][] = {
                {"Alabama", "Northern Flicker", "Camelia"},
                {"Alaska", "Willow PTarmigan", "ForgetMeNot"},
                {"Arizona", "Cactus Wren", "Saguaro Cactus Blossom"},
                {"Arkansas", "Mockingbird", "Apple Blossom"},
                {"California", "California Quail", "California Blossom"},
                {"Colorado", "Lark Bunting", "Rocking Mountain Columbine"},
                {"Connecticut", "American Robin", "Mountain Laurel"},
                {"Delaware", "Blue Hen Chicken", "Peach Blossom"},
                {"Flordia", "Mockingbird", "Orange Blossom"},
                {"Georgia", "Brown Thrasher", "Cherokee Rose"},
                {"Hawaii", "Nene", "Hibiscus"},
                {"Idaho", "Peregrine Falcon", "Mock Orange"},
                {"Illinois", " Northern Cardinal", "Purple Violet"},
                {"Indiana", "Northern Cardinal", "Peony"},
                {"Iowa", "Eastern Goldrinch", "Wild Prairie Rose"},
                {"Kansas", "Western Meadowlark", "Sunflower"},
                {"Kentucky", "Northen Cardinal", "Golden Rod"},
                {"Louisiana", "Brown Pelican", "Magnolia"},
                {"Maine", "Black-capped Chickadee", "White Pine Tassel and Cone"},
                {"Maryland", "Baltimore Oriole", "Black-Eyes Susan"},
                {"Massachusetts", "Black-Capped Chickadee", "Mayflower"},
                {"Michigan", "Robin Redbreast", "Apple Blossom"},
                {"Minnesota", "Common Loon", "Pink and White ladyslipper"},
                {"Mississippi", "Wood Duck", "Magnolia"},
                {"Missouri", "Eastern Bluebird", "Hawthorn"},
                {"Montana", "Western Meadowlark", "Bitterroot"},
                {"Nebraska", "Western Medowlark", "Goldenrod"},
                {"Nevada", "Mountain Bluebird", "Sagebush"},
                {"New Hampshire", "Purple Finch", "Purple Lilac"},
                {"New Jersey", "Eastern Goldfinch", "Violet"},
                {"New Mexico", "Roadrunner", "Yucca"},
                {"New York", "Bluebird", "Rose"},
                {"North Carolina", "Cardinal", "Flowering Dogwood"},
                {"North Dakota", "Western Meadowlark", "Wild Prairie Rose"},
                {"Ohio", "Cardinal", "Scarlet Carnation"},
                {"Oklahoma", "Scissor-tailed Flycatcher", "Mistletoe"},
                {"Oregon", "Western Medowlark", "Orange Grape"},
                {"Pennsylvania", "Ruffed Grouse", "Mountain Laurel"},
                {"Rhode Island", "Rhode Island Red", "Violet"},
                {"South Carolina", "Great Carolina Wren", "Yellow Jessamine"},
                {"South Dakota", "Ring-necked Pheasant", "Pasque Flower"},
                {"Tennessee", "Mocking Bird", "Iris"},
                {"Texas", "Mocking Bird", "Texas Bluebonnet"},
                {"Utah", "Common American Gull", "Sego Lily"},
                {"Vermont", "Hermit Thrush", "Red Clover"},
                {"Virginia", "Cardinal", "Flowering Dogwood"},
                {"Washington", "Willow Goldfinch", "Coast Rhodoendron"},
                {"West Virginia", "Cardinal", "Rhododendron"},
                {"Wisconsin", "Robin", "Violet"},
                {"Wyoming", "Western Medowlark", "Indian Paintbrush"},

        };


        Scanner input1 = new Scanner(System.in);
        ArrayList<String> userInputStates = new ArrayList<>();

        while (true) {
            System.out.println("Enter a State or None to exit: ");
            String states = input1.nextLine();

            if (states.trim().equalsIgnoreCase("None")) {
                System.out.println("**** Thank you *****\r\n" +
                        "A summary report for each State, Bird, and Flower is: ");
                //printing the userList
                printUserList(userInputStates);
                break;
            } else {

                //I moved this getBirdFlower method call to heere, because if not none , call from here
                int position = getBirdFlower(theState, states);
                if (position != -1) {
                    System.out.println("Bird: " + theState[position][1]);
                    System.out.println("Flower: " + theState[position][2]);
                    //Add user's found properties to arrayList.
                    userInputStates.add("- State: " + theState[position][0] + " Bird: " + theState[position][1] + " Flower: " + theState[position][2]);
                } else
                    System.out.println("Invalid state entered");
            }

        }


    }

    private static void printUserList(ArrayList<String> userInputStates) {
        //print userInputStates.
        for (String userState : userInputStates) {
            System.out.println(userState);
        }
    }

    public static int getBirdFlower(String theState[][], String state) {
        //you dont need to keep boolean , if found -> return current index
        for (int index = 0; index < theState.length; index++) {
            if (theState[index][0].equalsIgnoreCase(state))
                return index;
        }
        //if not found, return -1
        return -1;
    }

}

Sample output from code ;

Enter a State or None to exit: 
Wyoming
Bird: Western Medowlark
Flower: Indian Paintbrush
Enter a State or None to exit: 
Utah
Bird: Common American Gull
Flower: Sego Lily
Enter a State or None to exit: 
InvalidState
Invalid state entered
Enter a State or None to exit: 
none
**** Thank you *****
A summary report for each State, Bird, and Flower is: 
- State: Wyoming Bird: Western Medowlark Flower: Indian Paintbrush
- State: Utah Bird: Common American Gull Flower: Sego Lily
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for that drowny. I see how it prints all the states. how would i do this but from only as the input from the user?
Ofcourse , i added new part of code according to your wants. I stored the user valid inputs to list for printing after puts None keyword. Please check it, read comments on code @CreativeXtent
0

The trick is to store the user input as he types it. In this case I made my life easier by using an ArrayList but this can be done with normal arrays as well but there is more work managing the array. I also added some comment explaining each step.

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

        String theStates[][] = { { "Alabama", "Northern Flicker", "Camelia" },
                { "Alaska", "Willow PTarmigan", "ForgetMeNot" }, { "Arizona", "Cactus Wren", "Saguaro Cactus Blossom" },
                { "Arkansas", "Mockingbird", "Apple Blossom" },
                { "California", "California Quail", "California Blossom" },
                { "Colorado", "Lark Bunting", "Rocking Mountain Columbine" },
                { "Connecticut", "American Robin", "Mountain Laurel" },
                { "Delaware", "Blue Hen Chicken", "Peach Blossom" }, { "Flordia", "Mockingbird", "Orange Blossom" },
                { "Georgia", "Brown Thrasher", "Cherokee Rose" }, { "Hawaii", "Nene", "Hibiscus" },
                { "Idaho", "Peregrine Falcon", "Mock Orange" }, { "Illinois", " Northern Cardinal", "Purple Violet" },
                { "Indiana", "Northern Cardinal", "Peony" }, { "Iowa", "Eastern Goldrinch", "Wild Prairie Rose" },
                { "Kansas", "Western Meadowlark", "Sunflower" }, { "Kentucky", "Northen Cardinal", "Golden Rod" },
                { "Louisiana", "Brown Pelican", "Magnolia" },
                { "Maine", "Black-capped Chickadee", "White Pine Tassel and Cone" },
                { "Maryland", "Baltimore Oriole", "Black-Eyes Susan" },
                { "Massachusetts", "Black-Capped Chickadee", "Mayflower" },
                { "Michigan", "Robin Redbreast", "Apple Blossom" },
                { "Minnesota", "Common Loon", "Pink and White ladyslipper" },
                { "Mississippi", "Wood Duck", "Magnolia" }, { "Missouri", "Eastern Bluebird", "Hawthorn" },
                { "Montana", "Western Meadowlark", "Bitterroot" }, { "Nebraska", "Western Medowlark", "Goldenrod" },
                { "Nevada", "Mountain Bluebird", "Sagebush" }, { "New Hampshire", "Purple Finch", "Purple Lilac" },
                { "New Jersey", "Eastern Goldfinch", "Violet" }, { "New Mexico", "Roadrunner", "Yucca" },
                { "New York", "Bluebird", "Rose" }, { "North Carolina", "Cardinal", "Flowering Dogwood" },
                { "North Dakota", "Western Meadowlark", "Wild Prairie Rose" },
                { "Ohio", "Cardinal", "Scarlet Carnation" }, { "Oklahoma", "Scissor-tailed Flycatcher", "Mistletoe" },
                { "Oregon", "Western Medowlark", "Orange Grape" },
                { "Pennsylvania", "Ruffed Grouse", "Mountain Laurel" },
                { "Rhode Island", "Rhode Island Red", "Violet" },
                { "South Carolina", "Great Carolina Wren", "Yellow Jessamine" },
                { "South Dakota", "Ring-necked Pheasant", "Pasque Flower" }, { "Tennessee", "Mocking Bird", "Iris" },
                { "Texas", "Mocking Bird", "Texas Bluebonnet" }, { "Utah", "Common American Gull", "Sego Lily" },
                { "Vermont", "Hermit Thrush", "Red Clover" }, { "Virginia", "Cardinal", "Flowering Dogwood" },
                { "Washington", "Willow Goldfinch", "Coast Rhodoendron" },
                { "West Virginia", "Cardinal", "Rhododendron" }, { "Wisconsin", "Robin", "Violet" },
                { "Wyoming", "Western Medowlark", "Indian Paintbrush" },

        };

        Scanner scanner = new Scanner(System.in);
        ArrayList<String> visitedStates = new ArrayList<>();//using this to store the states already asked.

        while (true) {//loop forever... i'm not a fan of this
            System.out.println("Enter a State or None to exit: ");
            String state = scanner.nextLine().trim();//remove extra spaces and set the state

            if ("None".equalsIgnoreCase(state)) {//check if user is done
                System.out
                        .println("**** Thank you *****\r\n" + "A summary report for each State, Bird, and Flower is: ");
                for(String vistedState : visitedStates) {//loop though all the visited states
                    int stateIndex = getStateIndex(theStates, vistedState);//find the index of the visited states
                    System.out.println(Arrays.toString(theStates[stateIndex]));//print out the info for the state
                }
                break;//exit the while loop
            } else {//if the user is not done
                int position = getStateIndex(theStates, state);//find the state index
                if (position != -1) {//make sure it's a valid input
                    System.out.println("Bird: " + theStates[position][1]);//print out the bird
                    System.out.println("Flower: " + theStates[position][2]);//print out the flower
                    visitedStates.add(state);//add it to the list of visited states
                } else {
                    System.out.println("Invalid state entered");//bad input we don't print and don't store
                }
            }

        }

    }

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

}

Sample output:

    Enter a State or None to exit: 
    Idaho
    Bird: Peregrine Falcon
    Flower: Mock Orange
    Enter a State or None to exit: 
    Nevada
    Bird: Mountain Bluebird
    Flower: Sagebush
    Enter a State or None to exit: 
    None
    **** Thank you *****
    A summary report for each State, Bird, and Flower is: 
    [Idaho, Peregrine Falcon, Mock Orange]
    [Nevada, Mountain Bluebird, Sagebush]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.