write a Java console application that Create a text file called Data.txt. Within
ID: 3851690 • Letter: W
Question
write a Java console application that Create a text file called Data.txt. Within the file, use the first row for the name and data title, and use the second row for column headers. Within the columns, insure that the first column is a label column and other columns contain numeric data. In your application, read file Data.txt into parallel arrays, one for each column in the file. Create method printArrays to print the header row(s) and the (unsorted) data in formatted columns. Within method printArrays, also calculate and print the max, min, and average of each numeric column. Sort the data by the first numeric column using any sort method. Use method printArrays to print the (sorted) data again. Format all real numbers to one decimal place.
use the following data
Subject Bet city, New York Both sexes Male Female EDUCATIONAL ATTAINMENT (highest level) Population 18 to 24 years 10,635 5,249 5,386 Less than high school graduate 2,734 1,495 1,239 High school graduate (incl. equivalency) 3,351 1,710 1,641 Some college or associate degree 4,016 1,805 2,211 Bachelor's degree or higher 534 239 295 Population 25 years and over 95,949 45,772 50,177 Less than 5th grade 1,414 554 860 5th to 8th grade 4,658 2,037 2,621 9th to 12th grade, no diploma 16,099 7,697 8,402 High school graduate (incl. equivalency) 34,369 15,031 19,338 Some college credit, less than 1 year 7,837 3,532 4,305Explanation / Answer
Solution:
package fileread;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Fileread {
public static void printArray(String[] subject,int[] both,int[] male,int[] females,int n)
{
int max1=0,max2=0,max3=0;
for(int i=0;i<n;i++)
{
if(both[i]>max1)
max1=both[i];
if(male[i]>max2)
max2=male[i];
if(females[i]>max3)
max3=females[i];
System.out.println(max1);
System.out.println(max2);
System.out.println(max3);
}
int[] a=new int[100];
for(int i=0;i<n;i++)
{
a[i]=both[i];
}
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(a[j-1] > a[j]){
//swap elements
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
for(int i=0;i<n;i++)
{
for(int k=0;k<n;k++)
{
if(both[i]==a[k])
{
System.out.println(subject[k]+" "+both[k]+" "+male[k]+" "+females[k]);
}
}
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader infil = new BufferedReader(new FileReader("inp.txt"));
String[] subject=new String[100];
String nextLine;
int[] both=new int[100];
int[] male=new int[100];
int[] females=new int[100];
String[] inform;
int i=0;
while((nextLine = infil.readLine()) != null)
{
inform = nextLine.split(";");
subject[i] = inform[0];
both[i] = Integer.parseInt(inform[1]);
male[i]=Integer.parseInt(inform[2]);
females[i]=Integer.parseInt(inform[3]);
i++;
}
}
}
Input file:
Population 18 to 24 years;10635;5249;5386
Less than high school graduate;2734;1495;1239
High school graduate (incl. equivalency);3351;1710;1641
Some college or associate degree;4016;1805;2211
Bachelor's degree or higher;534;239;295
Population 25 years and over;95949;45772;50177
Less than 5th grade;1414;554;860
5th to 8th grade;4658;2037;2621
9th to 12th grade, no diploma;16099;7697;8402
High school graduate (incl. equivalency);34369;15031;19338
Some college credit, less than 1 year;7837;3532;4305
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.