Question

Java. Create a program called StatesArray that begins by asking the user how many states they...

Java.

Create a program called StatesArray that begins by asking the user how many states they wish to enter. Create an array of that size which stores USAState objects. Use a loop to get input necessary to create USAState objects and put them in the array. Then, do the following

- Output a list of all capital cities in the array.

- Prompt the user for a state name, then report whether or not the state is in the array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

StatesArray.java


import java.io.*;

public class StatesArray {

    public static void main(String[] args) throws Exception {

        int numState; //variable for number of states
        int k = 0;
        String state; //variable for user's state
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //to read inputs
        System.out.println("Enter number of states");
        numState = Integer.parseInt(input.readLine()); //to input how many states user wish to enter
        String[] USAState = new String[numState]; // initializing array of USAState objects
        System.out.println("Enter the States");
        for (int i = 0; i < USAState.length; i++) {
            //inputs the USAState objects as user wish to enter
            USAState[i] = input.readLine();
        }
        System.out.println("States in the array list is");
        for (int i = 0; i < numState; i++) {
            //outputs all states in the array
            System.out.println(USAState[i]);
        }
        System.out.println("Enter your State");
        state = input.readLine(); //prompt the user for a state name
        for (int i = 0; i < numState; i++, k++) {
            //comparing each state in the array with user's state
            if (state.equals(USAState[i])) {
                //prints if state is in the array
                System.out.println("Your state is in the list");
                break;
            }
        }
        if (k == numState) {
            //prints if state is not in the list
            System.out.println("Your state is not in the list");
        }
    }
}

OUTPUT

Enter number of states

4

Enter the States

Alabama

New York

Texas

California

States in the array list is

Alabama

New York

Texas

California

Enter your State

New York

Your state is in the list

Enter number of states

4

Enter the States

Alabama

New York

Texas

California

States in the array list is

Alabama

New York

Texas

California

Enter your State

Maryland

Your state is not in the list

Add a comment
Know the answer?
Add Answer to:
Java. Create a program called StatesArray that begins by asking the user how many states they...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named...

    FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named TextBookSort.java. Include these steps: Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price. This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java. Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for...

  • Create a Java program application that creates an ArrayList that holds String objects. It needs to...

    Create a Java program application that creates an ArrayList that holds String objects. It needs to prompt the user to enter the names of friends, which are then put into the ArrayList; allow the user to keep adding names until they enter "q" to quit. After input is complete, display a numbered list of all friends in the list and prompt the user to enter the number of the friend they wish to delete. After deleting that entry, output the...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Java Program: Make an application called that stores at least four different movies, a showtime (such...

    Java Program: Make an application called that stores at least four different movies, a showtime (such as "11:00am"), and the movie rating (such as "PG-13") in a two-dimensional array. Movies with multiple showtimes should be in the 2d array multiple times, once for each showing. Prompt the user for a movie name and output all showtimes for that film, or a message indicating it was not found.

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • java Create a program that utilized a multi-dimensional array for the user to input the name...

    java Create a program that utilized a multi-dimensional array for the user to input the name of a team and their number of wins. The user should be able to input up to 10 teams and the wins for each team.

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • Create a Java program which asks the user how many names they want to enter. The...

    Create a Java program which asks the user how many names they want to enter. The program should then instantiate an array that will hold this many names. The main method should call a method, getNames() to allow the user to enter the names. A for loop should be used in this method. Once the names are entered, the main method should call another method displayNames() which will use a while loop to display the names entered.

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT