I'm writing a java program for a school project that reads in data about covid-19 from a csv text file which has data in the following format:(date, state, fips code, # of cases, # of deaths). The problem is that the text file my professor gave us has over 60,000 lines of redundant data and the data I really need is on the final 55 lines of the text file. The file contains data about every single day from January 1st, 2021 to March 23rd 2023 and I only need the total amount of cases and total amount of deaths per U.S territory which essentially is located on the last 55 lines of the file. I already have most of the program working, I just need to make it so the data from those last 55 lines is read in so I can use it in some calculations. Is there any way for me to have my Buffered Reader only read the last 55 lines of the file?
5/1/2023: Thanks for all the answers, but I worked out a solution on my own yesterday. I just wrapped the while loop inside an if statement that would ensure that only lines beginning with the string "2023-03-23" would be read into the program.
Here's my code if anyone wants to look at it
import java.io.*;
import java.util.*;
public class StateCovidStats extends State
{
private int cases, deaths;
//default
public StateCovidStats()
{
}
//constructor
public StateCovidStats(String theName, int caseCount, int deathCount)
{
super(theName);
cases=caseCount;
deaths=deathCount;
}
public void displayStats()
{
System.out.println("----------------------------------------");
System.out.println(this.getName()+" Stats:");
System.out.println("Cases: "+this.getCases());
System.out.println("Deaths: "+this.getDeaths());
System.out.println("----------------------------------------");
}
//getters
public String getStateName()
{
return this.getName();
}
public int getCases()
{
return cases;
}
public int getDeaths()
{
return deaths;
}
//setters
public void setStateName(String newName)
{
this.setName(newName);
}
public void setCases(int caseCount)
{
cases=caseCount;
}
public void setDeaths(int deathCount)
{
deaths=deathCount;
}
public String toString()
{
String s="";
s+=this.getStateName()+" Cases: "+this.getCases()+" Deaths: "+this.getDeaths();
return s;
}
public static String findMaxCases(ArrayList<StateCovidStats> list)
{
int greatest=Integer.MIN_VALUE;
String nameOfMax="";
for(int i=0;i<list.size();i++)
{
if(list.get(i).getCases()>greatest) {
greatest=list.get(i).getCases();
nameOfMax=list.get(i).getName();
}
}
return nameOfMax;
}
public static String findMaxDeaths(ArrayList<StateCovidStats> list)
{
int greatest=Integer.MIN_VALUE;
String nameOfMax="";
for(int i=0;i<list.size();i++)
{
if(list.get(i).getDeaths()>greatest) {
greatest=list.get(i).getDeaths();
nameOfMax=list.get(i).getName();
}
}
return nameOfMax;
}
public static String findMinCases(ArrayList<StateCovidStats> list)
{
int least=Integer.MAX_VALUE;
String nameOfMin="";
for(int i=0;i<list.size();i++)
{
if(list.get(i).getCases()<least) {
least=list.get(i).getCases();
nameOfMin=list.get(i).getName();
}
}
return nameOfMin;
}
public static String findMinDeaths(ArrayList<StateCovidStats> list)
{
int least=Integer.MAX_VALUE;
String nameOfMin="";
for(int i=0;i<list.size();i++)
{
if(list.get(i).getDeaths()<least) {
least=list.get(i).getDeaths();
nameOfMin=list.get(i).getName();
}
}
return nameOfMin;
}
public static void main(String[] args)
{
//us-states-Jan-2020-through-March-2023
//sampleDataOld2021
String line="";
String start="";
long totDeath=0;
long totCase=0;
ArrayList<StateCovidStats> states=new ArrayList<StateCovidStats>();
try
{
BufferedReader br = new BufferedReader(new FileReader(".\\src\\/us-states-Jan-2020-through-March-2023.txt"));
while ((line = br.readLine()) != null )
{
String[] covid = line.split(","); // use comma as separator
totDeath+=Integer.parseInt(covid[4]);
totCase+=Integer.parseInt(covid[3]);
StateCovidStats temp=new StateCovidStats(covid[1],Integer.parseInt(covid[3]),Integer.parseInt(covid[4]));
for(int i=0; i<=states.size();i++)
{
try
{
if((states.get(i).getName().equalsIgnoreCase(temp.getName())))
{
break;
}
} //end of 2nd try
catch(IndexOutOfBoundsException e)
{
states.add(temp);
break;
}
}//end of for
} //end of while
br.close();
} //end of try
catch (IOException e)
{
e.printStackTrace();
} //end of catch
/*
for(int i=0; i<states.size();i++)
{
System.out.println(states.get(i).getName());
System.out.println("Cases: "+states.get(i).getCases());
System.out.println("Deaths: "+states.get(i).getDeaths());
System.out.println("------------------------------------");
}
*/
//output
String maxCase=findMaxCases(states);
String minCase=findMinCases(states);
String maxDeath=findMaxDeaths(states);
String minDeath=findMinDeaths(states);
int maxCaseNum=0;
int minCaseNum=0;
int maxDeathNum=0;
int minDeathNum=0;
for(int i=0; i<states.size();i++)
{
if(states.get(i).getName().equals("New Jersey"))
{
states.get(i).displayStats();
}
if(states.get(i).getName().equals(maxCase))
{
maxCaseNum=states.get(i).getCases();
}
if(states.get(i).getName().equals(minCase))
{
minCaseNum=states.get(i).getCases();
}
if(states.get(i).getName().equals(maxDeath))
{
maxDeathNum=states.get(i).getDeaths();
}
if(states.get(i).getName().equals(minDeath))
{
minDeathNum=states.get(i).getDeaths();
}
}
System.out.println("State With Most Cases: "+maxCase+", "+maxCaseNum+" Cases");
System.out.println("State With Least Cases: "+minCase+", "+minCaseNum+" Cases");
System.out.println("State With Most Deaths: "+maxDeath+", "+maxDeathNum+" Deaths");
System.out.println("State With Least Deaths: "+minDeath+", "+minDeathNum+" Deaths");
System.out.println("Total US Cases: "+totCase);
System.out.println("Total US Deaths: "+totDeath);
System.out.println("------------------------------------");
System.out.println("AVG State Cases: "+totCase/55);
System.out.println("AVG State Deaths: "+totDeath/55);
for(int i=0; i<states.size();i++)
{
if(states.get(i).getName().equals("New Jersey"))
{
states.get(i).displayStats();
}
}
} //end of main
} //end of class
public class State
{
private String stateName,timeZone;
private int population;
private double density;
//default constructor
public State()
{
}
//constructor
public State(String theName)
{
stateName=theName;
}
//name getter+setter
public String getName()
{
return stateName;
}
public void setName(String theName)
{
stateName=theName;
}
}