JAVA How to add a timer to this code All I need it to do is
JAVA: How to add a timer to this code? All I need it to do is count and print the entire length of time in seconds the program runs from start to finish, from execution to termination. It doesn\'t matter where in the code as long as the timer counts the entire time the program is running. The program runs endlessly until the user enters \'q\' or \'Q\' and its just for pulling data from an excel file and comparing fields/indexes. It is only a 2 class program. I know I need an object, a method likely for the start, and to pass a parameter but I generally hit an error each time.
// Driver.java
import java.io.*;
import java.util.Scanner;
public class Driver {
static void getStart() {
float start = System.currentTimeMillis();
float stop = System.currentTimeMillis();
float eTime = stop - start;
float TotalTime = eTime / 1000;
System.out.println(\"Time taken was: \" + TotalTime + \"seconds\");
}
// User Menu Standard Output Print
static void getMenu() {
float start = System.currentTimeMillis();
String printIt =
\"\ Enter the number of the question you want answered. Enter \'Q\' to quit the program :\ \" +
\"1. What were the percentages in population growth for each consecutive year from 1994 to 2013?\ \" +
\"2. What year had the highest murder rate?\ \" +
\"3. What year had the lowest murder rate?\ \" +
\"4. What year had the highest robbery rate?\ \" +
\"5. What year had the lowest robbery rate?\ \" +
\"6. What percentage did motor vehicle theft based crimes change between 1998 and 2012?\ \" +
\"7. What is the difference in the number of rapes when comparing the year 1998 to the year 2010?\ \" +
\"8. What percentage did assault based crimes change from 1994 to 2013?\ \" +
\"Q. Quit the program, and also lowercase \'q\' can quit the program.\";
System.out.print(printIt);
}
// Take input from user and store and return it for usage.
static String getFile() {
String n = \"\";
Scanner input = new Scanner(System.in);
System.out.print(\"\ \");
System.out.print(\"\ Enter your selection: \");
n = input.next();
return n;
}
// This method figures and displays the percentage of population growth for each proceeding year.
static void getPopulationPercent(USCrimeClass[] arr3) {
float popGrowth;
for(int i = 0; i < arr3.length - 1; i++) {
popGrowth = 100 * (float)(arr3[i+1].getPopulation() - arr3[i].getPopulation()) / arr3[i].getPopulation();
System.out.println(\"[\" + arr3[i].getYear() + \"-\" + arr3[i+1].getYear() + \"]: \" + String.format(\"%.4f\",popGrowth) + \" %\");
}
}
// This method returns the highest murder rate of any year.
static int getHighMurderRate(USCrimeClass[] arr3) {
int someYear = 0;
float highNum = 0;
for (USCrimeClass crime : arr3) {
if (crime.getMurderRate() > highNum) {
highNum = crime.getMurderRate();
someYear = crime.getYear();
}
}
return someYear;
}
// This method returns the lowest murder rate of any year.
static int getLowMurderRate(USCrimeClass[] arr3) {
int lowYear = 0;
float lowNum = arr3[0].getMurderRate();
for (USCrimeClass crime : arr3) {
if (crime.getMurderRate() < lowNum) {
lowNum = crime.getMurderRate();
lowYear = crime.getYear();
}
}
return lowYear;
}
// This method returns the highest robbery rate of any year.
static int getHighRobberyRate(USCrimeClass[] arr3) {
int highYear = 0;
float highNum = 0;
for (USCrimeClass crime : arr3) {
if (crime.getRobberyRate() > highNum) {
highNum = crime.getRobberyRate();
highYear = crime.getYear();
}
}
return highYear;
}
// This method returns the lowest robbery rate of any year.
static int getLowRobberyRate(USCrimeClass[] arr3) {
int lowYear = 0;
float lowNum = arr3[0].getRobberyRate();
for (USCrimeClass crime : arr3) {
if (crime.getRobberyRate() < lowNum) {
lowNum = crime.getRobberyRate();
lowYear = crime.getYear();
}
}
return lowYear;
}
// This methods returns the percentage of change in crimes involving motor vehicle theft from 1998 - 2012.
static float getChangeInMotor(USCrimeClass[] arr3) {
float ques6 = 0;
int crime1 = arr3[4].getMotorCrime();
int crime2 = arr3[18].getMotorCrime();
ques6 = (float)(crime2 - crime1) * 100 / crime1;
return ques6;
}
// This method returns the difference in rapes when comparing 1998 to the year 2010.
static int getDifferenceInRapes(USCrimeClass[] arr3) {
int someNum = 0;
int rapeCrime1 = arr3[4].getRapeCrime();
int rapeCrime2 = arr3[17].getRapeCrime();
someNum = rapeCrime2 - rapeCrime1;
return someNum;
}
// This methods returns the percentage of change in crimes involving assault crimes from 1994 to 2013.
static float getChangeInAssault(USCrimeClass[] arr3) {
float ques8 = 0;
float assaultCrime1 = arr3[0].getAssaultRate();
float assaultCrime2 = arr3[19].getAssaultRate();
ques8 = (assaultCrime2 - assaultCrime1) * 100 / assaultCrime1;
return ques8;
}
// Loop with a Switch to decide the user\'s choice.
static void makeDecision(USCrimeClass[] arr3) {
String n = \"\";
do {
getMenu();
n = getFile();
switch(n) {
case \"1\":
System.out.print(\"\ \");
getPopulationPercent(arr3);
break;
case \"2\":
System.out.print(\"\ \");
System.out.println(\"The murder rate was highest in the year: \" + getHighMurderRate(arr3));
break;
case \"3\":
System.out.print(\"\ \");
System.out.println(\"The murder rate was lowest in the year: \" + getLowMurderRate(arr3));
break;
case \"4\":
System.out.print(\"\ \");
System.out.println(\"The robbery rate was highest in the year: \" + getHighRobberyRate(arr3));
break;
case \"5\":
System.out.print(\"\ \");
System.out.println(\"The robbery rate was lowest in the year: \" + getLowRobberyRate(arr3));
break;
case \"6\":
System.out.print(\"\ \");
System.out.println(\"Total percentage change in motor vehicle theft between 1998 through 2012 is: \" + String.format(\"%.4f\",getChangeInMotor(arr3)) +\" %\");
break;
case \"7\":
System.out.print(\"\ \");
System.out.println(\"The difference in the number of rapes from the year 1998 when compared to the year 2010 is: \" + getDifferenceInRapes(arr3));
break;
case \"8\":
System.out.print(\"\ \");
System.out.println(\"The total percent change in assault based crimes between from 1994 to 2013 is: \" + String.format(\"%.4f\",getChangeInAssault(arr3)) + \" %\");
break;
case \"Q\":
case \"q\":
System.out.print(\"\ \");
System.out.println(\"Thank you for trying the US Crimes Statistics Program.\");
break;
default:
System.out.print(\"\ \");
System.out.println(\"Error: Invalid choice selected!! Try again.\ \");
}
}while(!n.equalsIgnoreCase(\"Q\"));
}
// Stores Excel File Table Data in an Array
static USCrimeClass[] readFile(String filename) {
String p = \"\";
int i = 0;
USCrimeClass[] Arr1 = new USCrimeClass[20];
try {
Scanner readIt = new Scanner(new FileInputStream(new File(filename)));
readIt.nextLine();
while(readIt.hasNextLine()) {
p = readIt.nextLine();
String[] Arr2 = p.split(\",\");
Arr1[i] = new USCrimeClass(Integer.parseInt(Arr2[0]));
Arr1[i].setPopulation(Integer.parseInt(Arr2[1]));
Arr1[i].setViolentCrime(Integer.parseInt(Arr2[2]));
Arr1[i].setViolentRate(Float.parseFloat(Arr2[3]));
Arr1[i].setMurderCrime(Integer.parseInt(Arr2[4]));
Arr1[i].setMurderRate(Float.parseFloat(Arr2[5]));
Arr1[i].setRapeCrime(Integer.parseInt(Arr2[6]));
Arr1[i].setRapeRate(Float.parseFloat(Arr2[7]));
Arr1[i].setRobberyCrime(Integer.parseInt(Arr2[8]));
Arr1[i].setRobberyRate(Float.parseFloat(Arr2[9]));
Arr1[i].setAssaultCrime(Integer.parseInt(Arr2[10]));
Arr1[i].setAssaultRate(Float.parseFloat(Arr2[11]));
Arr1[i].setPropertyCrime(Integer.parseInt(Arr2[12]));
Arr1[i].setPropertyRate(Float.parseFloat(Arr2[13]));
Arr1[i].setBurglaryCrime(Integer.parseInt(Arr2[14]));
Arr1[i].setBurglaryRate(Float.parseFloat(Arr2[15]));
Arr1[i].setTheftCrime(Integer.parseInt(Arr2[16]));
Arr1[i].setTheftRate(Float.parseFloat(Arr2[17]));
Arr1[i].setMotorCrime(Integer.parseInt(Arr2[18]));
Arr1[i].setMotorRate(Float.parseFloat(Arr2[19]));
i +=1;
}
}
catch(IOException ex) {
System.out.println(ex);
}
return Arr1;
}
// An introduction to the program and tells the user the correct command line instruction if entered wrongly.
public static void main(String[] args) {
System.out.println(\"********************Welcome to the US Crime Statistical Application********************\");
USCrimeClass[] excelTable = readFile(args[0]);
makeDecision(excelTable);
if(args.length != 1) {
System.out.println(\"Please enter: C:>\" + \"java Driver Crime.csv\");
}
}
}
//
//
// USCrimeClass.java
public class USCrimeClass {
private int year = -1;
private int population = -1;
private int violentCrime = -1;
private int murderCrime = -1;
private int rapeCrime = -1;
private int robberyCrime = -1;
private int assaultCrime = -1;
private int propertyCrime = -1;
private int burglaryCrime = -1;
private int theftCrime = -1;
private int motorCrime =1;
private float violentRate = -1;
private float murderRate = -1;
private float rapeRate = -1;
private float robberyRate = -1;
private float assaultRate = -1;
private float propertyRate = -1;
private float burglaryRate = -1;
private float theftRate = -1;
private float motorRate = -1;
// Constructor
public USCrimeClass(int year) {
this.year = year;
}
// Setters
public void setYear(int year) {
this.year = year;
}
public void setPopulation(int population) {
this.population = population;
}
public void setViolentCrime(int violentCrime) {
this.violentCrime = violentCrime;
}
public void setViolentRate(float violentRate) {
this.violentRate = violentRate;
}
public void setMurderCrime(int murderCrime) {
this.murderCrime = murderCrime;
}
public void setMurderRate(float murderRate) {
this.murderRate = murderRate;
}
public void setRapeCrime(int rapeCrime) {
this.rapeCrime = rapeCrime;
}
public void setRapeRate(float rapeRate) {
this.rapeRate = rapeRate;
}
public void setRobberyCrime(int robberyCrime) {
this.robberyCrime = robberyCrime;
}
public void setRobberyRate(float robberyRate) {
this.robberyRate = robberyRate;
}
public void setAssaultCrime(int assaultCrime) {
this.assaultCrime = assaultCrime;
}
public void setAssaultRate(float assaultRate) {
this.assaultRate = assaultRate;
}
public void setPropertyCrime(int propertyCrime) {
this.propertyCrime = propertyCrime;
}
public void setPropertyRate(float propertyRate) {
this.propertyRate = propertyRate;
}
public void setBurglaryCrime(int burglaryCrime) {
this.burglaryCrime = burglaryCrime;
}
public void setBurglaryRate(float burglaryRate) {
this.burglaryRate = burglaryRate;
}
public void setTheftCrime(int theftCrime) {
this.theftCrime = theftCrime;
}
public void setTheftRate(float theftRate) {
this.theftRate = theftRate;
}
public void setMotorCrime(int motorCrime) {
this.motorCrime = motorCrime;
}
public void setMotorRate(float motorRate) {
this.motorRate = motorRate;
}
// Getters
public int getYear() {
return this.year;
}
public int getPopulation() {
return this.population;
}
public int getViolentCrime() {
return this.violentCrime;
}
public float getViolentRate() {
return this.violentRate;
}
public int getMurderCrime() {
return this.murderCrime;
}
public float getMurderRate() {
return this.murderRate;
}
public int getRapeCrime() {
return this.rapeCrime;
}
public float getRapeRate() {
return this.rapeRate;
}
public int getRobberyCrime() {
return this.robberyCrime;
}
public float getRobberyRate() {
return this.robberyRate;
}
public int getAssaultCrime() {
return this.assaultCrime;
}
public float getAssaultRate() {
return this.assaultRate;
}
public int getPropertyCrime() {
return this.propertyCrime;
}
public float getPropertyRate() {
return this.propertyRate;
}
public int getBurglaryCrime() {
return this.burglaryCrime;
}
public float getBurglaryRate() {
return this.burglaryRate;
}
public int getTheftCrime() {
return this.theftCrime;
}
public float getTheftRate() {
return this.theftRate;
}
public int getMotorCrime() {
return this.motorCrime;
}
public float getMotorRate() {
return this.motorRate;
}
}
//
//
// Crime.csv(excel file)
| Year | Population | Violent crime | Violent crime rate | Murder and nonnegligent manslaughter | Murder and nonnegligent manslaughter rate | Rape | Rape rate | Robbery | Robbery rate | Aggravated assault | Aggravated assault rate | Property crime | Property crime rate | Burglary | Burglary rate |
| 1994 | 2.6E+08 | 1857670 | 713.6 | 23326 | 9 | 102216 | 39.3 | 618949 | 237.8 | 1113179 | 427.6 | 12131873 | 4660.2 | 2712774 | 1042.1 |
| 1995 | 2.63E+08 | 1798792 | 684.5 | 21606 | 8.2 | 97470 | 37.1 | 580509 | 220.9 | 1099207 | 418.3 | 12063935 | 4590.5 | 2593784 | 987 |
| 1996 | 2.65E+08 | 1688540 | 636.6 | 19645 | 7.4 | 96252 | 36.3 | 535594 | 201.9 | 1037049 | 391 | 11805323 | 4451 | 2506400 | 945 |
| 1997 | 2.68E+08 | 1636096 | 611 | 18208 | 6.8 | 96153 | 35.9 | 498534 | 186.2 | 1023201 | 382.1 | 11558475 | 4316.3 | 2460526 | 918.8 |
| 1998 | 2.7E+08 | 1533887 | 567.6 | 16974 | 6.3 | 93144 | 34.5 | 447186 | 165.5 | 976583 | 361.4 | 10951827 | 4052.5 | 2332735 | 863.2 |
| 1999 | 2.73E+08 | 1426044 | 523 | 15522 | 5.7 | 89411 | 32.8 | 409371 | 150.1 | 911740 | 334.3 | 10208334 | 3743.6 | 2100739 | 770.4 |
| 2000 | 2.81E+08 | 1425486 | 506.5 | 15586 | 5.5 | 90178 | 32 | 408016 | 145 | 911706 | 324 | 10182584 | 3618.3 | 2050992 | 728.8 |
| 2001 | 2.85E+08 | 1439480 | 504.5 | 16037 | 5.6 | 90863 | 31.8 | 423557 | 148.5 | 909023 | 318.6 | 10437189 | 3658.1 | 2116531 | 741.8 |
| 2002 | 2.88E+08 | 1423677 | 494.4 | 16229 | 5.6 | 95235 | 33.1 | 420806 | 146.1 | 891407 | 309.5 | 10455277 | 3630.6 | 2151252 | 747 |
| 2003 | 2.91E+08 | 1383676 | 475.8 | 16528 | 5.7 | 93883 | 32.3 | 414235 | 142.5 | 859030 | 295.4 | 10442862 | 3591.2 | 2154834 | 741 |
| 2004 | 2.94E+08 | 1360088 | 463.2 | 16148 | 5.5 | 95089 | 32.4 | 401470 | 136.7 | 847381 | 288.6 | 10319386 | 3514.1 | 2144446 | 730.3 |
| 2005 | 2.97E+08 | 1390745 | 469 | 16740 | 5.6 | 94347 | 31.8 | 417438 | 140.8 | 862220 | 290.8 | 10174754 | 3431.5 | 2155448 | 726.9 |
| 2006 | 2.99E+08 | 1435123 | 479.3 | 17309 | 5.8 | 94472 | 31.6 | 449246 | 150 | 874096 | 292 | 10019601 | 3346.6 | 2194993 | 733.1 |
| 2007 | 3.02E+08 | 1422970 | 471.8 | 17128 | 5.7 | 92160 | 30.6 | 447324 | 148.3 | 866358 | 287.2 | 9882212 | 3276.4 | 2190198 | 726.1 |
| 2008 | 3.04E+08 | 1394461 | 458.6 | 16465 | 5.4 | 90750 | 29.8 | 443563 | 145.9 | 843683 | 277.5 | 9774152 | 3214.6 | 2228887 | 733 |
| 2009 | 3.07E+08 | 1325896 | 431.9 | 15399 | 5 | 89241 | 29.1 | 408742 | 133.1 | 812514 | 264.7 | 9337060 | 3041.3 | 2203313 | 717.7 |
| 2010 | 3.09E+08 | 1251248 | 404.5 | 14722 | 4.8 | 85593 | 27.7 | 369089 | 119.3 | 781844 | 252.8 | 9112625 | 2945.9 | 2168459 | 701 |
| 2011 | 3.12E+08 | 1206005 | 387.1 | 14661 | 4.7 | 84175 | 27 | 354746 | 113.9 | 752423 | 241.5 | 9052743 | 2905.4 | 2185140 | 701.3 |
| 2012 | 3.14E+08 | 1217057 | 387.8 | 14856 | 4.7 | 85141 | 27.1 | 355051 | 113.1 | 762009 | 242.8 | 9001992 | 2868 | 2109932 | 672.2 |
| 2013 | 3.16E+08 | 1163146 | 367.9 | 14196 | 4.5 | 79770 | 25.2 | 345031 | 109.1 | 724149 | 229.1 | 8632512 | 2730.7 | 1928465 | 610 |
Solution
Use currentTimeMillis() to get the current time.
long startTime = System.currentTimeMillis();
//.....your program....
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);









