Question

JAVA: File USCapitals.txt is here: Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento...

JAVA:

File USCapitals.txt is here:

Alabama, Montgomery

Alaska, Juneau

Arizona, Phoenix

Arkansas, Little Rock

California, Sacramento

Colorado, Denver

Connecticut, Hartford

Delaware, Dover

Florida, Tallahassee

Georgia, Atlanta

Hawaii, Honolulu

Idaho, Boise

Illinois, Springfield

Indiana, Indianapolis

Iowa, Des Moines

Kansas, Topeka

Kentucky, Frankfort

Louisiana, Baton Rouge

Maine, Augusta

Maryland, Annapolis

Massachusettes, Boston

Michigan, Lansing

Minnesota, Saint Paul

Mississippi, Jackson

Missouri, Jefferson City

Montana, Helena

Nebraska, Lincoln

Nevada, Carson City

New Hampshire, Concord

New Jersey, Trenton

New York, Albany

New Mexico, Santa Fe

North Carolina, Raleigh

North Dakota, Bismark

Ohio, Columbus

Oklahoma, Oklahoma City

Oregon, Salem

Pennslyvania, Harrisburg

Rhode Island, Providence

South Carolina, Columbia

South Dakota, Pierre

Tennessee, Nashville

Texas, Austin

Utah, Salt Lake City

Vermont, Montpelier

Virginia, Richmond

Washington, Olympia

West Virginia, Charleston

Wisconsin, Madison

Wyoming, Cheyenne

The file contains the names of all states in the United States and the capital of the state in question.

The format of the file is:

State name, capital name

So state and capital name are comma-separated. Both state and capital name can be several words, such as New York and Little Rock.

One state, capital per line.

Create a program that reads the file and creates a Map with key and value.

Then offer the user to enter a state name and print the capital of the state, or error message if the state does not exist.

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

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;

public class StateCapitals {
   public static void main(String[] args) throws Exception {
       BufferedReader br = new BufferedReader(new FileReader("USCapitals.txt"));
       HashMap<String, String> map = new HashMap<String, String>();
       String line = br.readLine();
       //reading and putting all states and capitals into map
       while (line != null) {
           String arr[] = line.split(",");
           map.put(arr[0], arr[1]);
           line = br.readLine();
       }
       //reading state name from user
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter state name: ");
       String s = sc.next();
       //printing capital for given state
       System.out.println(s + "'s capital is " + map.get(s));
       ;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
JAVA: File USCapitals.txt is here: Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento...
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
  • Write a C# windows forms program to create a States and Capitals guessing game. A method...

    Write a C# windows forms program to create a States and Capitals guessing game. A method in your program should generate a random number between 1 and 50. Your program should use this number to represent one of the 50 U.S states and then display the name of that state. The user will guess the capital of that state. Another method in your program should get the users answer and check to see whether it is correct. Yet another method...

  • Location Percent Obesity Median Income Alabama 33.5 49,936 Alaska 29.7 68,734 Arizona 28.9 62,283 Arkansas 35.9...

    Location Percent Obesity Median Income Alabama 33.5 49,936 Alaska 29.7 68,734 Arizona 28.9 62,283 Arkansas 35.9 49,781 California 24.7 70,489 Colorado 21.3 73,034 Connecticut 26.3 72,812 Delaware 30.7 65,012 District of Columbia 21.7 85,750 Florida 26.2 54,644 Georgia 30.5 55,821 Hawaii 22.1 80,108 Idaho 28.9 58,728 Illinois 29.3 70,145 Indiana 32.7 59,892 Iowa 30.9 68,718 Kansas 31.3 63,938 Kentucky 31.6 54,555 Louisiana 34.9 49,973 Maine 28.2 58,663 Maryland 29.6 86,223 Massachusetts 23.3 86,345 Michigan 30.7 60,449 Minnesota 27.6 71,817 Mississippi...

  • Location Percent Obesity Median Income Alabama 33.5 49,936 Alaska 29.7 68,734 Arizona 28.9 62,283 Arkansas 35.9...

    Location Percent Obesity Median Income Alabama 33.5 49,936 Alaska 29.7 68,734 Arizona 28.9 62,283 Arkansas 35.9 49,781 California 24.7 70,489 Colorado 21.3 73,034 Connecticut 26.3 72,812 Delaware 30.7 65,012 District of Columbia 21.7 85,750 Florida 26.2 54,644 Georgia 30.5 55,821 Hawaii 22.1 80,108 Idaho 28.9 58,728 Illinois 29.3 70,145 Indiana 32.7 59,892 Iowa 30.9 68,718 Kansas 31.3 63,938 Kentucky 31.6 54,555 Louisiana 34.9 49,973 Maine 28.2 58,663 Maryland 29.6 86,223 Massachusetts 23.3 86,345 Michigan 30.7 60,449 Minnesota 27.6 71,817 Mississippi...

  • State Ownership (%) Income ($) Alabama 66.7 36000 Alaska 62.5 57624 Arizona 62.9 41759 Arkansas 61.0...

    State Ownership (%) Income ($) Alabama 66.7 36000 Alaska 62.5 57624 Arizona 62.9 41759 Arkansas 61.0 32558 California 53.0 52154 Colorado 63.5 51950 Connecticut 66.2 60871 Delaware 70.7 48134 District of Columbia 41.5 49161 Florida 64.7 41651 Georgia 61.2 39360 Hawaii 55.2 51669 Idaho 69.1 42798 Illinois 63.9 48890 Indiana 65.5 40325 Iowa 66.7 46741 Kansas 61.4 40737 Kentucky 64.6 38684 Louisiana 65.6 41453 Maine 67.8 43522 Maryland 65.3 60206 Massachusetts 60.7 55393 Michigan 68.1 42014 Minnesota 67.7 52110 Mississippi...

  • The data contained in the file named StateUnemp show the unemployment rate in March 2011 and...

    The data contained in the file named StateUnemp show the unemployment rate in March 2011 and the unemployment rate in March 2012 for every state.† State      Unemploy- ment Rate March 2011        Unemploy- ment Rate March 2012 Alabama              9.3          7.3 Alaska 7.6          7.0 Arizona                 9.6          8.6 Arkansas              8.0          7.4 California             11.9       11.0 Colorado              8.5          7.8 Connecticut        9.1          7.7 Delaware             7.3          6.9 Florida 10.7      ...

  • School Air Force Falcons Akron Zips Alabama Crimson Tide Arizona State Sun Devils Arizona Wildcats Arkansas...

    School Air Force Falcons Akron Zips Alabama Crimson Tide Arizona State Sun Devils Arizona Wildcats Arkansas Razorbacks Arkansas State Red Wolves Army Black Knights Auburn Tigers Ball State Cardinals Baylor Bears Boise State Broncos Boston College Eagles Bowling Green Falcons Buffalo Bulls BYU Cougars California Golden Bears Central Michigan Chippewas Charlotte 49ers Cincinnati Bearcats Clemson Tigers Colorado Buffaloes Colorado State Rams Connecticut Huskies Duke Blue Devils East Carolina Pirates Eastern Michigan Eagles Florida International Golden Panthers Florida Atlantic Owls Florida...

  • JAVA Create a Governor class with the following attributes: name : String party - char (the...

    JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...

  • 1.1 Fuel Economy. (mpg) of model year 2016 motor vehicles Here is a small part of a data set that...

    1.1 Fuel Economy. (mpg) of model year 2016 motor vehicles Here is a small part of a data set that describes the fuel economy in miles per gallon Make and Vehicle Class Transmission Number of City Highway Annual Fuel Model Type Cylinders mpg mpg Cost Subaru Im Compact preza Nissan Juke Small station Manual Manual 4 25 34 $900 4 28 34 1,100 wagon Midsize Hyundai Elantra GT Automatic 24 $950 19 Chevrolet Im Large pala Automatic 29 1,150 The...

  • 26. (For this question use the data file Per Capita Link). A researcher interested in examining...

    26. (For this question use the data file Per Capita Link). A researcher interested in examining how a state's per capita income may be affected by the percent of the state's adult population that has not completed high school and the percent of the state's population that is foreign bom. part a: Write down an appropriate model for this researcher to estimate. Clearly identify what is the response variable and what are the explanatory variables. part b: Using regression analysis,...

  • CPS 276 DOC02-- Assignment 02 Cost of Living Calculator Your client maintains job search website. As...

    CPS 276 DOC02-- Assignment 02 Cost of Living Calculator Your client maintains job search website. As a service on their website, they need an application that compares costs of living between different metropolitan areas. Client will pay $780 for a complete application. Background Information: A person living in an area with a high cost of living will need higher income to maintain the same standard of living as someone living in an area with a low cost of living. For...

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
ADVERTISEMENT