Java programing. Produce the following output from text file. the text file \"Ba
ID: 3859756 • Letter: J
Question
Java programing. Produce the following output from text file.
the text file "Baby name" is :
Ada 154 196 224 331 445 627 962 0 0
Sam 99 131 168 236 278 380 467 408 46
Samantha 0 0 0 0 272 107 26 5 7
Samara 0 0 0 0 0 0 0 0 886
Samir 0 0 0 0 0 0 920 0 798
The out put Bold is user input.
name? ada
1920: 154,
1930: 196,
1940: 244,
1950: 331,
1960: 445,
1970: 627,
1980: 962,
1990: 0,
2000: 0
//
Then, you need to save the statistics into a result file for that baby name. For the example above, the result file should be named as “Ada.txt”, which should contain contents in the following format exactly:
Ada,
1920: 154,
1930: 196,
1940: 244,
1950: 331,
1960: 445,
1970: 627,
1980: 962,
1990: 0,
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class BabyNameMain {
public static void main(String[] args) {
try {
readBabyName();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readBabyName() throws IOException {
File file = new File("Baby Name.txt");
InputStreamReader is = new FileReader(file);
BufferedReader br = new BufferedReader(is);
String babyStatistics = null;
while((babyStatistics = br.readLine()) != null) {
outputBabyNameStatistics(babyStatistics.split(" "));
}
is.close();
br.close();
}
private static void outputBabyNameStatistics(String[] babyStatistics) throws IOException {
File file = new File(babyStatistics[0]);
OutputStreamWriter os = new FileWriter(file);
os.write("1920: " + babyStatistics[1] + ", ");
os.write("1930: " + babyStatistics[2] + ", ");
os.write("1940: " + babyStatistics[3] + ", ");
os.write("1950: " + babyStatistics[4] + ", ");
os.write("1960: " + babyStatistics[5] + ", ");
os.write("1970: " + babyStatistics[6] + ", ");
os.write("1980: " + babyStatistics[7] + ", ");
os.write("1990: " + babyStatistics[8] + ", ");
os.write("2000: " + babyStatistics[9]);
os.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.