Question

In: Computer Science

Write a java program that will read a file called stateinfo.txt and will store the information...

Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows.

State                Capital

---------                         -----------

NSW               Sydney

VIC                 Melbourne

WA                 Perth

TAS                 Tasmania

QLD                Brisbane

SA                   Adelaide

The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should display the name of the capital for that particular state. Assume that the name of the state that a user may enter already exists in the stateinfo.txt file.

Solutions

Expert Solution

SourceCode:

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

public class SearchCapitals {
    public static void main(String args[]){
        //Declaring the variables
        String stateName;
        //Creating the scanner object for getting input from user
        Scanner scanner = new Scanner(System.in);
        //Creating the Map objects
        Map<String,String> map=new HashMap<String,String>();
        //Creating the BufferedReader objects for reading the text file
        BufferedReader reader;
        //Reading the the text file
        try {
            reader = new BufferedReader(new FileReader("stateinfo.txt"));
            String line = reader.readLine();
            //Reading the file line by line until the end of the file is reached
            while (line != null) {
                //Spliting the line in words
                String data[] = line.split(" ");
                map.put(data[0],data[1]);
                //Puting the data to the map
                //read next line
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Please enter name of the state: ");
        stateName = scanner.nextLine();
        System.out.println("Capital of " + stateName + " is " + map.get(stateName));
    }
}

OUTPUT:


Related Solutions

Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Question6: Write a program to read the file 202010mid.txt, store the content in a list of...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of tuples, then print the list. [6 marks] Question7: Write Python code to do the following operations using request library. [12 marks] Check if the following webpage is available or not: https://edition.cnn.com/news.html [4 marks] Send a get request to the following webpage and show the result: http://api.open-notify.org/iss-pass.json [4 marks] The webpage from part 2 is expecting some parameters as shown from the results. Now create...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
Write a menu-driven program to read 3 employees' information from a text file called “ EmployeeFile”...
Write a menu-driven program to read 3 employees' information from a text file called “ EmployeeFile” for this example but that can be edited to add more employees later. Each employee has a name field, ID fields, and a Salary Record. The Salary Record has weekly hours, rate per hour, gross salary, Tax, and net salary field. 1.1 Implement the project with queue using pointers 1.2 Write push and pop functions for queue using pointers C++ EmployeeField.txt example John_Doe 123456...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT