In: Computer Science
Could you please also give me a code that does not use the string split method but instead gives an input and output file that you have to compile using "type output.txt" in java.
This is the code:
Record.java
public class Record {
private String stateCode, districCode, districtName;
private int totalPopulation, childPopulation,
childPovertyPopulation;
private String miscStats;
public Record(String stateCode, String districCode, String
districtName, int totalPopulation,
int childPopulation, int childPovertyPopulation, String miscStats)
{
this.stateCode = stateCode;
this.districCode = districCode;
this.districtName = districtName;
this.totalPopulation = totalPopulation;
this.childPopulation = childPopulation;
this.childPovertyPopulation = childPovertyPopulation;
this.miscStats = miscStats;
}
public String getStateCode() {
return stateCode;
}
public String getDistricCode() {
return districCode;
}
public String getDistrictName() {
return districtName;
}
public int getTotalPopulation() {
return totalPopulation;
}
public int getChildPopulation() {
return childPopulation;
}
public int getChildPovertyPopulation() {
return childPovertyPopulation;
}
public String getMiscStats() {
return miscStats;
}
public double getChildPovertyPercent()
{
return(((double)getChildPovertyPopulation() /
(double)getChildPopulation()) * 100);
}
}
ReadNWrite.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadNWrite {
public ReadNWrite(String inFileName, String outFileName)
{
ArrayList<Record> recs = readFromFile(inFileName);
writeToFile(outFileName, recs);
}
private ArrayList<Record> readFromFile(String fileName)
{
Scanner fileReader;
ArrayList<Record> recs = new ArrayList<>();
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(",");
String stateCode = data[0];
String distCode = data[1];
String distName = data[2];
int population = Integer.parseInt(data[3]);
int childPopulation = Integer.parseInt(data[4]);
int childPovertyPopulation = Integer.parseInt(data[5]);
String miscStats = data[6];
recs.add(new Record(stateCode, distCode, distName, population,
childPopulation, childPovertyPopulation
, miscStats));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Cannot locate file: " + fileName);
System.exit(0);
}
return recs;
}
private void writeToFile(String fileName, ArrayList<Record>
recs)
{
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(fileName), true);
pw = new PrintWriter(fw);
for(Record rec : recs)
{
pw.write(rec.getStateCode() + "," + rec.getTotalPopulation() + ","
+ rec.getChildPopulation()
+ "," + rec.getChildPovertyPopulation() + "," +
rec.getChildPovertyPercent()
+ System.lineSeparator());
}
pw.flush();
fw.close();
pw.close();
} catch (IOException ex) {
System.out.println("Error in writing to file: " + fileName);
System.exit(0);
}
}
}
DisplayStatsMain.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class DisplayStatsMain {
private static final String INPUT_FILE = "PovertyData.txt";
private static final String OUTPUT_FILE =
"PopulationSummary.txt";
public static void main(String[]args)
{
new ReadNWrite(INPUT_FILE, OUTPUT_FILE);
ArrayList<String> recs =
readStatsFromFile(OUTPUT_FILE);
displayTabularStats(recs);
}
private static ArrayList<String> readStatsFromFile(String
fileName)
{
Scanner fileReader;
ArrayList<String> recs = new ArrayList<>();
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
recs.add(fileReader.nextLine().trim());
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println("Cannot locate file: " + fileName);
System.exit(0);
}
return recs;
}
private static void displayTabularStats(ArrayList<String>
recs)
{
System.out.printf("%5s %20s %20s %30s
%20s\n-------------------------------------------------------------------"
+ "--------------------------------\n",
"State", "Population", "Child Population", "Child Poverty
Population", "% Child Poverty");
for(String s : recs)
{
String[] data = s.split(",");
String state = data[0];
int totalPop = Integer.parseInt(data[1]);
int childPop = Integer.parseInt(data[2]);
int childPovertyPop = Integer.parseInt(data[3]);
double childPovertyPercent = Double.parseDouble(data[4]);
System.out.printf("%5s %20d %20d %30d %20.2f\n", state, totalPop,
childPop, childPovertyPop, childPovertyPercent);
}
System.out.println();
}
}
Record.java
public class Record {
private String stateCode, districCode, districtName;
private int totalPopulation, childPopulation,
childPovertyPopulation;
private String miscStats;
public Record(String stateCode, String districCode, String
districtName, int totalPopulation,
int childPopulation, int childPovertyPopulation, String miscStats)
{
this.stateCode = stateCode;
this.districCode = districCode;
this.districtName = districtName;
this.totalPopulation = totalPopulation;
this.childPopulation = childPopulation;
this.childPovertyPopulation = childPovertyPopulation;
this.miscStats = miscStats;
}
public String getStateCode() {
return stateCode;
}
public String getDistricCode() {
return districCode;
}
public String getDistrictName() {
return districtName;
}
public int getTotalPopulation() {
return totalPopulation;
}
public int getChildPopulation() {
return childPopulation;
}
public int getChildPovertyPopulation() {
return childPovertyPopulation;
}
public String getMiscStats() {
return miscStats;
}
public double getChildPovertyPercent() {
return (((double) getChildPovertyPopulation() / (double)
getChildPopulation()) * 100);
}
}
ReadNWrite.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadNWrite {
public ReadNWrite(String inFileName, String outFileName) {
ArrayList<Record> recs = readFromFile(inFileName);
writeToFile(outFileName, recs);
}
private ArrayList<Record> readFromFile(String fileName)
{
Scanner fileReader;
ArrayList<Record> recs = new ArrayList<>();
try {
fileReader = new Scanner(new File(fileName));
while (fileReader.hasNextLine()) {
String line = fileReader.nextLine().trim();
String[] data = splitIntoTokens(line, ',');
String stateCode = data[0];
String distCode = data[1];
String distName = data[2];
int population = Integer.parseInt(data[3]);
int childPopulation = Integer.parseInt(data[4]);
int childPovertyPopulation = Integer.parseInt(data[5]);
String miscStats = data[6];
recs.add(new Record(stateCode, distCode, distName, population,
childPopulation, childPovertyPopulation,
miscStats));
}
fileReader.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Cannot locate file: " + fileName);
System.exit(0);
}
return recs;
}
private void writeToFile(String fileName,
ArrayList<Record> recs) {
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(fileName));
pw = new PrintWriter(fw);
for (Record rec : recs) {
pw.write(rec.getStateCode() + "," + rec.getTotalPopulation() + ","
+ rec.getChildPopulation()
+ "," + rec.getChildPovertyPopulation() + "," +
rec.getChildPovertyPercent()
+ System.lineSeparator());
}
pw.flush();
fw.close();
pw.close();
} catch (IOException ex) {
System.out.println("Error in writing to file: " + fileName);
System.exit(0);
}
}
// this method breaks a string into tokens based on a delimiter
specified by the parameter 'delimiter'
private static String[] splitIntoTokens(String s, char
delimiter)
{
int count = 0;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++)
{
if (ch[i] == delimiter)
{
count++;
}
}
String dummy = "";
int index = 0;
String[] tokenBag = new String[count + 1];
for (int i = 0; i < ch.length; i++)
{
if (ch[i] == delimiter)
{
tokenBag[index++] = dummy;
dummy = "";
}
else
{
dummy = dummy + ch[i];
}
}
tokenBag[index] = dummy;
return tokenBag;
}
}
DisplayStatsMain.java (Main class)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class DisplayStatsMain {
private static final String INPUT_FILE =
"PovertyData.txt";
private static final String OUTPUT_FILE =
"PopulationSummary.txt";
public static void main(String[] args) {
new ReadNWrite(INPUT_FILE, OUTPUT_FILE);
ArrayList<String> recs =
readStatsFromFile(OUTPUT_FILE);
displayTabularStats(recs);
}
private static ArrayList<String> readStatsFromFile(String
fileName) {
Scanner fileReader;
ArrayList<String> recs = new ArrayList<>();
try {
fileReader = new Scanner(new File(fileName));
while (fileReader.hasNextLine()) {
recs.add(fileReader.nextLine().trim());
}
fileReader.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Cannot locate file: " + fileName);
System.exit(0);
}
return recs;
}
private static void displayTabularStats(ArrayList<String>
recs) {
System.out.printf("%5s %20s %20s %30s
%20s\n-------------------------------------------------------------------"
+ "--------------------------------\n",
"State", "Population", "Child Population", "Child Poverty
Population", "% Child Poverty");
for (String s : recs) {
String[] data = splitIntoTokens(s, ',');
String state = data[0];
int totalPop = Integer.parseInt(data[1]);
int childPop = Integer.parseInt(data[2]);
int childPovertyPop = Integer.parseInt(data[3]);
double childPovertyPercent = Double.parseDouble(data[4]);
System.out.printf("%5s %20d %20d %30d %20.2f\n", state,
totalPop, childPop, childPovertyPop, childPovertyPercent);
}
System.out.println();
}
// this method breaks a string into tokens based on a delimiter
specified by the parameter 'delimiter'
private static String[] splitIntoTokens(String s, char
delimiter)
{
int count = 0;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++)
{
if (ch[i] == delimiter)
{
count++;
}
}
String dummy = "";
int index = 0;
String[] tokenBag = new String[count + 1];
for (int i = 0; i < ch.length; i++)
{
if (ch[i] == delimiter)
{
tokenBag[index++] = dummy;
dummy = "";
}
else
{
dummy = dummy + ch[i];
}
}
tokenBag[index] = dummy;
return tokenBag;
}
}
********************************************************************* SCREENSHOT ******************************************************
Note: If you are not satisfied with the solution, feel free to explain your query in the comment section.