For class, I have to write a program using arrays where someone can look up popular baby names during a certain time frame. (Ex: The most popular boy and girl name from 1890-1910 is ....)
I think the program is decent, but it runs really slow. I don't know if it is just because of my code, or having a simple program look through a large file; but is there a way to speed up the process?
import java.io.*;
import java.util.*;
import java.net.URL;
public class BabyNamesMainDriver {
// ===========================================================================================
private static void process(Scanner sc, String[] args) throws Exception {
// The two arrays
ArrayList<BabyNameClass> boys = new ArrayList<BabyNameClass>();
ArrayList<BabyNameClass> girls = new ArrayList<BabyNameClass>();
System.out.println("Enter a start year between 1880 and 2016:");// start year
int startYear = sc.nextInt();
if (startYear < 1880 || startYear > 2016)// Error check
System.out.println("ERROR: Invalid start year.\n");
else
System.out.println("Enter a end year. Must be less then 2016 but greater than the start year :");// End year
int endYear = sc.nextInt();
if (endYear < 1880 || endYear > 2016)// Error check
System.out.println("ERROR: Invalid end year. \n");
else
System.out.println("Enter the number of baby names to list. ");// Number of baby names
int numOfNames = sc.nextInt();
// if (topBabies <= 0);//Error Check
// System.out.println("ERROR: Invalid number of names. \n");
ReadBabyNames(startYear, endYear, boys, girls);// Reads file info
// Header for top girl names
System.out.print("\nTop " + numOfNames + " Girl names:\n");
for (int i = 0; i < numOfNames; i++) {
System.out.println(girls.get(i));
}
// Header for top boy names
System.out.print("\nTop " + numOfNames + " Boy names:\n");
for (int i = 0; i < numOfNames; i++) {
System.out.println(boys.get(i));
}
sc.nextLine();
}
// ===========================================================================================
private static void ReadBabyNames(int startYear, int endYear, ArrayList<BabyNameClass> boys,
ArrayList<BabyNameClass> girls) throws IOException, Exception {
System.out.println("Please stand by...");
for (int year = startYear; year <= endYear; year++) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(
new URL("https://cs.stcc.edu/~silvestri/babynames/yob" + year + ".txt").openStream());// file from
// URL
sc.useDelimiter("\\s*,\\s*|\\s+");
while (sc.hasNextLine()) {
String name = sc.next();
String sex = sc.next();
int number = sc.nextInt();
sc.nextLine();
BabyNameClass babies = new BabyNameClass(name, number);
Collections.sort(boys);
Collections.sort(girls);
// Getting number of lil' babies
if (sex.equalsIgnoreCase("F")) {
int index = 1;
index = girls.indexOf(babies);
if (index == -1)
girls.add(babies);
else
girls.get(index).addToAmount(number);
} else {
int index = 1;
index = boys.indexOf(babies);
if (index == -1)
boys.add(babies);
else
boys.get(index).addToAmount(number);
}
}
}
}
// ===========================================================================================
public static void main(String args[]) throws Exception {
final String TITLE = "Baby Name Ranking";
final String CONTINUE_PROMPT = "\nDo this again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
// ===========================================================================================
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
}
2nd class
public class BabyNameClass implements Comparable<BabyNameClass> {
// ==========================
private String name;
private int num;
// ===========================================================================================
public BabyNameClass(String name, int num) {
this.name = name;
this.num = num;
}
// ===========================================================================================
public String getName() {
return this.name;
}
// ===========================================================================================
public int getNum() {
return this.num;
}
// ===========================================================================================
public void addToAmount(int num) {
this.num += num;
}
// ===========================================================================================
public void setAmount(int num) {
this.num = num;
}
// ===========================================================================================
public boolean equals(Object lak) {
if (lak == null)
return false;
if (this == lak)
return true;
if (getClass() != lak.getClass())
return false;
BabyNameClass Gu = (BabyNameClass) lak;
if (name == null) {
if (Gu.name != null)
return false;
} else if (!name.equals(Gu.name))
return false;
return true;
}
// ===========================================================================================
public int compareTo(BabyNameClass arg0) {
if (this.num < arg0.num)
return 1;
if (this.num > arg0.num)
return -1;
if (this.name.compareTo(arg0.name) > 0)
return 1;
if (this.name.compareTo(arg0.name) < 0)
return -1;
else
return 0;
}
// ===========================================================================================
public String toString() {
return " " + this.num + " babies were named " + this.name;
}
}
-
For reference it takes about 2 seconds to load those text files in my browser. Some of the code might be able to be improved, but if you always have to load the text files over the wire, that's probably the bulk of the issue.Alexis Murray– Alexis Murray2017年11月07日 02:19:38 +00:00Commented Nov 7, 2017 at 2:19
1 Answer 1
You're sorting two arraylists every single time you read a new line from the txt file. Why? Sorting is very cpu-intensive. Especially when those txt files you are using are huge.
Just try and move the sort out of the while loop.
-
Omg, you're a genius. Thank you!DarkProph– DarkProph2017年11月07日 02:29:25 +00:00Commented Nov 7, 2017 at 2:29