Question

In: Computer Science

Write a program in Java that reads a file containing data about the changing popularity of...

Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file called ‘population.txt’ has been given to you. Your program should prompt the user for a name and search the file for that name. If the name is found, the program should display the data about the name on the screen. If the name is not found, then display the message ‘Name not found in the file’

Your program should at-least have the following method:

checkName(): A method that takes the baby name as a parameter and returns true if the name exist and false otherwise.

Sample Output:

Name? Sam

Statistics on the name “Sam”

1900: 58

1910: 69

1920: 99

1930: 131

Solutions

Expert Solution

The file was not attached in question, So I created one and saved at location C:\population.txt

population.txt

I created an infinite loop and used the input "end" for stop program
Program Result:

Code: Text Code Also Included in End

Code:

package javaapplication7;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.stream.Stream;

public class NamePopularity {
    // Container For Store Names Data
    private static HashMap<String, ArrayList> namesData = new HashMap<>();
    private static Scanner scanner = new Scanner(System.in);
  
    public static void main(String args[]) throws IOException{
      String fileName = "population.txt";
      String filePath = "C:\\" + fileName;
    
      // Reading File
      Stream<String> lines = Files.lines(Paths.get(filePath));
      //Saving File Data
      lines.forEach(s -> saveData(s));
    
      // Type end for exist from loop
      while(true){
          System.out.println("Type Name For Search:");
          String name = scanner.next();
        
          if(name.equals("end")){
              System.out.println("Ending Program");
              break;
          }
          if(checkName(name)){
              printNameData(name);
          }else{
              System.out.println("Name Not Found in File");
          }  
      }
  
    }
  
//    Storing Names Data In Hash Map using Key as Person Name And Popularity As Array
    private static void saveData(String data){
        String[] splited = data.split("\\s+");
        ArrayList<String> popularity = new ArrayList<>();
        for(int i = 1; i < splited.length; i++){
            popularity.add(splited[i]);
        }
        namesData.put(splited[0], popularity);
    }
  
    // Checking Name
    private static Boolean checkName(String name){
        if(namesData.containsKey(name)){
            return true;
        }else{
            return false;
        }
    }
    private static void printNameData(String name){
        ArrayList<String> popularity = namesData.get(name);
        System.out.println("Statistics of name \""+name +"\"");
        int startYear = 1900;
        for(String s: popularity){
            System.out.println(startYear + ": " + s);
            startYear += 10;
        }
    }
}


Related Solutions

Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
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 Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT