Question: Hi, I need solution to this assignment. I'm not sure what else to do. This code isn't producing the right output. I've spent so much
Hi,
I need solution to this assignment. I'm not sure what else to do. This code isn't producing the right output. I've spent so much time trying to fix it and I need help.
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.SortedSet;
import java.io.FileNotFoundException;
import java.util.Set;
import java.util.TreeSet;
class Main {
public static final int START = 1900;
public static final String FILE = "/Users/Megan/Downloads/names.txt";
public static String getName() {
System.out.println("This program allows you to search through the "
+ "data from the Social Security Administration "
+ "to see how popular a particular name has been "
+ "since " + START);
System.out.print("Name? ");
Scanner obj = new Scanner(System.in);
String name = obj.next();
return name;
}
public static String getData(String name) throws FileNotFoundException {
Scanner obj = new Scanner(new File(FILE));
String str;
while (obj.hasNextLine()) {
str = obj.nextLine();
if (str.toLowerCase().startsWith(name.toLowerCase())) {
return str;
}
}
return null;
}
public static void Display(String data) {
String str[] = data.trim().split(" ");
System.out.println("Statistics on name \""+str[0]+"\"");
int y = START;
for (int i = 1; i < str.length; i++) {
System.out.println(y + " " + str[i]);
y++;
}
}
public static void main(String[] args) throws FileNotFoundException {
String name = getName();
String line = getData(name);
if (line == null) {
System.out.println("\""+name + "\" not found");
} else {
Display(line);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
