For some reason, I am getting null values for record no. 60 in my output file I
ID: 3785767 • Letter: F
Question
For some reason, I am getting null values for record no. 60 in my output file I attached below, would be great if you can fix the error. My code will be below as well.
1. The 2 fields mentioned above in “p1artists.txt” file.
2. The Constructor that accepts the 2 fields when the record of an artist is created.
3. Getters and setters for the 2 fields.
4. The toString() method.
5. Test the program with the following data
. ArtistID Artist Name
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
Modify the above program according to the following:
1. Read the input file “p1artists.txt” instead of hardcoding the values.
2. Use the exception handler to ensure that artist ID is numeric.
3. If the input is incorrect, print the line number and its contents, but do not stop.
4. Send the output to a text file named “p1artists_out1.txt”.
iii. Enhance the above program for the following:
1. A tab-delimited text file named “p1arts.txt” contains the following record formats:
ArtID Title ArtistID AppraisedValue
1001 Red Rock Mountain 50 18000
1002 Offerings 52 10000
1003 Spring Flowers 12 2400
2. Assume that this file contains no error. You are going to read “p1artists.txt” into an array, and then process “p1arts.txt” to add the artist name to the output. Name this output file “p1arts_out.txt”. Example follows:
ArtID Title Artist ArtistID Appraised Value
1001 Red Rock Mountain Mogan 50 18000
1002 Offerings Novarre 52 10000
1003 Spring Flowers Chico 12
3. Use an exception handler if the ArtistID does not exist in the “p1artists.txt” file. However, you still need to write this record to the output file.
4. In the end, include the total number of the Art Works and Artists, and the total appraised value.
Here is p1artists.txt
1 Acconci
2 Ames
3 Aserty
4 Baron
5 Battenberg
6 Bindner
7 Blain
8 Blum
9 Budd
10 Cardenas
11 Carpenter
12 Chico
13 Colvin
14 Cox
15 Cridler
16 Curtis
17 Dawson
18 DiGrigoro
19 Dill
20 Edwards
21 Fleming
22 Fratt
23 Garber
24 Garin
25 Giama
26 Gilhooly
27 Gonzales
28 Guys
29 Hamend
30 Higgins
31 Hofmann
32 Ibe
33 Indiana
34 Ingraham
35 Irvin
36 Kerrihard
37 Kollasch
38 Kritz
39 Lerman
40 Long
41 Lowney
42 Lundquist
43 Lutes
44 Maglich
45 McGraw
46 McIver
47 Meglech
48 Metz
49 Miller
50 Mogan
51 Motherwell
52 Novarre
53 Odiezma
54 Ortega
55 Parker
56 Penn
57 Pierobon
58 Prinzen
59 Quiroz
60 Rath
Here is p1arts.txt
1001 Red Rock Mountain 50 18000
1002 Offerings 52 10000
1003 Spring Flowers 12 2400
1004 Seeking Shelter 64 52000
1005 The Hang 18 8000
1006 House Remembered 32 700
1007 Homage to the Ancestors 82 1200
1008 End of the Path 26 1900
1009 Amen 28 3000
1010 Untitled (land with adobe) 71 800
1011 Eve 19 975
1012 Man on Horseback 74 8000
1013 Superstitions 3 78000
1014 Plenty 45 500
1015 Punch 46 10000
1016 Untitled 65 6000
1017 Brittlecone 6 1300
1018 Mountain Scene 8 2500
1019 The White Heart 61 9300
1020 Untitled (Man holding coat) 73 3000
1021 Bead Wall 3 14000
1022 The Cowboy 69 4200
1023 Shooting the Rapids 47 1300
1024 Spirit and Nature 48 592
1025 Profile of a Woman 68 625
1026 Untitled (couple) 66 4000
1027 Mountain Climber 47 4700
1028 Tired Cowboy 50 4700
1029 Horseshoe Falls 31 15000
1030 Ash Bench 28 13000
1031 Inside/Out 34 3500
1032 Rising Sun 42 2000
1033 Untitled (Woman abstract) 77 2500
1034 Beaver Pole Jumble 3 28000
1035 Nature/Nurture 47 1300
1036 Blackhawk 5 25500
1037 Floating World 21 2350
1038 Spring Flowers 1 800
1039 Treachery 14 20000
1040 Night on the Praire 47 1300
1041 Night Version 29 3800
1042 Coffee on the Trail 2 7544
1043 Creosote Bushes 28 18000
1044 Mexican Fiesta 43 14000
1045 Leaf Patterns 38 2100
1046 Immediate Gratification 33 1500
1047 Medicine Man 44 2500
1048 Comfy Chair 57 800
1049 Buttercup with Red Lip 7 400
1050 Cattle Ranch 1 10000
1051 Night Version 36 7000
1052 American Rodeo 16 3500
1053 Blue Eyed Indian 6 40000
1054 Snake Charmer 50 4500
1055 Starlit Evening 9 9500
1056 Cavalry Is Coming 6 1900
1057 Untitled 66 4500
1058 The Gathering 60 250
1059 Dwelling 17 16000
1060 Story Sticks 42 650
1061 Untitled Mural 78 3520
1062 Cowboy and Saddle 41 18000
1063 Asleep in the Garden 3 110000
1064 Spirit Columns 51 7000
1065 Moonlite 47 1300
1066 Untitled (still life) 76 19500
1067 Owl in Flight 49 7000
1068 Moonlight 50 9750
1069 Renaissance 50 5500
1070 Beginnings 4 27500
1071 Ride the Rapids 79 300
1072 Funnel 24 4500
1073 Dancing in the Light 15 4000
1074 Storm on the Rise 55 8000
1075 Western Boots and Spurs 6 6000
1076 Ride the Bronco 79 1500
1077 Bull Riding 6 5200
1078 Chuckwagon 28 32000
1079 Carrying the Mail 62 8000
1080 The Dust Behind 59 18000
1081 Coming Under Fire 13 650
1082 Spring Flowers 29 20000
1083 Untitled 64 2500
1084 Crossing the Platt River 23 2200
1085 Traces 63 20000
1086 Untitled (desert landscape) 67 18000
1087 Three Woman 81 20000
1088 Lessons 37 3700
1089 Life Lessons 53 4125
1090 Off the Grid 11 8000
1091 Stone Palette 54 11500
1092 Dressing Up 47 1300
1093 Antelopes 62 12500
1094 Life Is Sweet 39 25000
1095 The Spirit 61 20000
1096 Ceremonial Sticks 10 15000
1097 Untitled (Sea) 75 2800
1098 Sweet Project 56 592
1099 Watch That Rattler 20 900
1100 Hungry Cowboys 38 750
1101 The Red Door 58 10000
1102 Crying Hats 14 10000
1103 Trail End 1 8000
1104 Untitled 70 1800
1105 Meteor Show 80 10000
1106 Horse Corral 40 12500
1107 Striking It Rich 35 1750
1108 Untitled Mural 77 400
1109 Friends 22 16000
1110 Three Sisters 62 6500
1111 Untitled (man and crucifix) 72 3200
1112 Dark Canyon 27 8000
1113 Shadow House 50 5500
1114 Storytelling at the Campfire 50 18000
1115 Starry Night 25 8500
1116 Apache Warrior 30 23000
My code:
import java.io.*;
import java.util.*;
public class ArtistTest {
private String artistName; // name of artist
private int artistId; //id of artist
// constructor
ArtistTest(int number, String name) {
this.artistId = number;
this.artistName = name;
}
//return the artistName
public String getArtistName() {
return artistName;
}
//set the artistName
public void setArtistName(String artistName) {
this.artistName = artistName;
}
//return the artistId
public int getArtistId() {
return artistId;
}
// set the artistId
public void setArtistId(int artistId) {
this.artistId = artistId;
}
// toString method
public String toString() {
return this.artistId + " " + this.artistName;
}
// main method
public static void main(String[] args) throws IOException {
// Testing the program with the following sample data.
ArtistTest artist1 = new ArtistTest(1, "Acconci");
ArtistTest artist2 = new ArtistTest(2, "Ames");
ArtistTest artist3 = new ArtistTest(3, "Aserty");
ArtistTest artist4 = new ArtistTest(4, "Baron");
ArtistTest artist5 = new ArtistTest(5, "Battenberg");
System.out.println(artist1.toString());
System.out.println(artist2.toString());
System.out.println(artist3.toString());
System.out.println(artist4.toString());
System.out.println(artist5.toString());
// reading records from file and putting in to an Array
List<ArtistTest> artistList = null;
try {
artistList = new ArrayList<ArtistTest>();
ArtistsArray(artistList);
} catch (IOException ioe) {
System.out.println("Exception while reading from file!");
}
// Reading the second file and checks the artist
// id present in the second file & array
try {
populateFile(artistList);
} catch (IOException ioe) {
System.out.println("Exception while reading from file!");
}
} // end main
/**
* Method to create output file by checking the artistId present in the
* second file
*/
private static void populateFile(List<ArtistTest> artistList)
throws IOException {
// Getting the file to process
Scanner myP1ArtsFile = new Scanner(new FileReader(
"C:/Users/patel/Desktop/p1arts.txt"));
// Creating output file
File outPutfile = new File(
"C:/Users/patel/Desktop/p1artists_out1.txt");
FileWriter fstream = new FileWriter(outPutfile);
BufferedWriter outputWriter = new BufferedWriter(fstream);
String artId = null;
String title = null;
String artistName = null;
int artistId = 0;
String apprisedValue = null;
outputWriter.write("ArtID Title ArtistID ArtistName Appraised Value ");
outputWriter.newLine();
int numberOfArtists = 1;
int totalValue = 0;
// Loop until the P1Arts file has next line
while (myP1ArtsFile.hasNext())
{
String string = null;
StringBuffer sb = new StringBuffer();
// ArtId
artId = myP1ArtsFile.next();
// loops until the ArtTitle is read
while (null != (string = myP1ArtsFile.next()))
{
if (!string.matches("^[\d ]*$"))
{
sb.append(string + " ");
}
else
{
// ArtTitle appending
title = sb.toString() + " ";
boolean artistFound = false;
// Checking weather the artist is present in the p1arts file
for (ArtistTest artist : artistList) {
artistId = artist.getArtistId();
int id = Integer.parseInt(string);
if (artistId == id) {
artistName = artist.getArtistName();
artistFound = true;
break;
}
}
if (!artistFound) {
System.out.println("Artist id " + string
+ " not present in the P1Arts file.");
artistFound = true;
}
// Appraised Value
apprisedValue = myP1ArtsFile.next();
totalValue += Integer.valueOf(apprisedValue);
String titlefinal = null;
String artistfinal = null;
// Padding spaces for columns in output file
if (title.length() < 70)
{
titlefinal = String.format("%-30s", title);
}
// Padding spaces in output file
if (null != artistName && artistName.length() < 30) {
artistfinal = String.format("%-10s", artistName);
}
// Writing the information into the output file
outputWriter.write(artId + " " + titlefinal + " "
+ artistId + " " + artistfinal + " "
+ apprisedValue);
artistName = null;
break;
}
}
// New line in output file
outputWriter.newLine();
numberOfArtists++;
}
outputWriter.newLine();
outputWriter.newLine();
outputWriter.newLine();
outputWriter.write("The total number of the Art Works and Artists: " + numberOfArtists
+ " and the total appraised value: " + totalValue);
outputWriter.flush(); // Clearing the writer
outputWriter.close(); // Closing the writer
myP1ArtsFile.close(); // Closing the file
}
/**
* This method is used to read the contents from p1artist file and populate
* array object
*/
private static void ArtistsArray(List<ArtistTest> artistList)
throws IOException
{
// Get the file
Scanner myfile = new Scanner(new FileReader(
"C:/Users/patel/Desktop/p1artists.txt"));
// boolean for line number and error scenario
int lineNumber = 1;
boolean error = false;
// Out file creation
File outPutfile = new File(
"C:/Users/patel/Desktop/p1artists_out1.txt");
// Creating a file stream to accept stream data
FileWriter fstream = new FileWriter(outPutfile);
// Writer to write the data to file
BufferedWriter outputWriter = new BufferedWriter(fstream);
String artistName = null;
int artistId = 0;
// loops until the file has next line
while (myfile.hasNext())
{
try
{
artistId = Integer.parseInt(myfile.next());
} catch (NumberFormatException nfe) {
error = true;
}
try {
if (null != (artistName = myfile.nextLine()))
artistList.add(new ArtistTest(artistId, artistName));
outputWriter.write(artistId + " " + artistName);
outputWriter.newLine();
} catch (Exception e) {
System.out.println("End of line Missing.");
}
// Numerical Error handling
if (error) {
System.out.println("Line Number : " + lineNumber
+ " Artist Id: " + artistId + " ArtistName: "
+ artistName);
System.out.println("Artist id should be in numeric format only.");
error = false;
}
lineNumber++;
}
myfile.close();// Closing the file
outputWriter.flush();// Clearing the writer
outputWriter.close();// Closing the writer
}
}
Output File (what I got):
ArtID Title ArtistID ArtistName Appraised Value
1001 Red Rock Mountain 50 Mogan 18000
1002 Offerings 52 Novarre 10000
1003 Spring Flowers 12 Chico 2400
1004 Seeking Shelter 60 null 52000
1005 The Hang 18 DiGrigoro 8000
1006 House Remembered 32 Ibe 700
1007 Homage to the Ancestors 60 null 1200
1008 End of the Path 26 Gilhooly 1900
1009 Amen 28 Guys 3000
1010 Untitled (land with adobe) 60 null 800
1011 Eve 19 Dill 975
1012 Man on Horseback 60 null 8000
1013 Superstitions 3 Aserty 78000
1014 Plenty 45 McGraw 500
1015 Punch 46 McIver 10000
1016 Untitled 60 null 6000
1017 Brittlecone 6 Bindner 1300
1018 Mountain Scene 8 Blum 2500
1019 The White Heart 60 null 9300
1020 Untitled (Man holding coat) 60 null 3000
1021 Bead Wall 3 Aserty 14000
1022 The Cowboy 60 null 4200
1023 Shooting the Rapids 47 Meglech 1300
1024 Spirit and Nature 48 Metz 592
1025 Profile of a Woman 60 null 625
1026 Untitled (couple) 60 null 4000
1027 Mountain Climber 47 Meglech 4700
1028 Tired Cowboy 50 Mogan 4700
1029 Horseshoe Falls 31 Hofmann 15000
1030 Ash Bench 28 Guys 13000
1031 Inside/Out 34 Ingraham 3500
1032 Rising Sun 42 Lundquist 2000
1033 Untitled (Woman abstract) 60 null 2500
1034 Beaver Pole Jumble 3 Aserty 28000
1035 Nature/Nurture 47 Meglech 1300
1036 Blackhawk 5 Battenberg 25500
1037 Floating World 21 Fleming 2350
1038 Spring Flowers 1 Acconci 800
1039 Treachery 14 Cox 20000
1040 Night on the Praire 47 Meglech 1300
1041 Night Version 29 Hamend 3800
1042 Coffee on the Trail 2 Ames 7544
1043 Creosote Bushes 28 Guys 18000
1044 Mexican Fiesta 43 Lutes 14000
1045 Leaf Patterns 38 Kritz 2100
1046 Immediate Gratification 33 Indiana 1500
1047 Medicine Man 44 Maglich 2500
1048 Comfy Chair 57 Pierobon 800
1049 Buttercup with Red Lip 7 Blain 400
1050 Cattle Ranch 1 Acconci 10000
1051 Night Version 36 Kerrihard 7000
1052 American Rodeo 16 Curtis 3500
1053 Blue Eyed Indian 6 Bindner 40000
1054 Snake Charmer 50 Mogan 4500
1055 Starlit Evening 9 Budd 9500
1056 Cavalry Is Coming 6 Bindner 1900
1057 Untitled 60 null 4500
1058 The Gathering 60 Rath 250
1059 Dwelling 17 Dawson 16000
1060 Story Sticks 42 Lundquist 650
1061 Untitled Mural 60 null 3520
1062 Cowboy and Saddle 41 Lowney 18000
1063 Asleep in the Garden 3 Aserty 110000
1064 Spirit Columns 51 Motherwell 7000
1065 Moonlite 47 Meglech 1300
1066 Untitled (still life) 60 null 19500
1067 Owl in Flight 49 Miller 7000
1068 Moonlight 50 Mogan 9750
1069 Renaissance 50 Mogan 5500
1070 Beginnings 4 Baron 27500
1071 Ride the Rapids 60 null 300
1072 Funnel 24 Garin 4500
1073 Dancing in the Light 15 Cridler 4000
1074 Storm on the Rise 55 Parker 8000
1075 Western Boots and Spurs 6 Bindner 6000
1076 Ride the Bronco 60 null 1500
1077 Bull Riding 6 Bindner 5200
1078 Chuckwagon 28 Guys 32000
1079 Carrying the Mail 60 null 8000
1080 The Dust Behind 59 Quiroz 18000
1081 Coming Under Fire 13 Colvin 650
1082 Spring Flowers 29 Hamend 20000
1083 Untitled 60 null 2500
1084 Crossing the Platt River 23 Garber 2200
1085 Traces 60 null 20000
1086 Untitled (desert landscape) 60 null 18000
1087 Three Woman 60 null 20000
1088 Lessons 37 Kollasch 3700
1089 Life Lessons 53 Odiezma 4125
1090 Off the Grid 11 Carpenter 8000
1091 Stone Palette 54 Ortega 11500
1092 Dressing Up 47 Meglech 1300
1093 Antelopes 60 null 12500
1094 Life Is Sweet 39 Lerman 25000
1095 The Spirit 60 null 20000
1096 Ceremonial Sticks 10 Cardenas 15000
1097 Untitled (Sea) 60 null 2800
1098 Sweet Project 56 Penn 592
1099 Watch That Rattler 20 Edwards 900
1100 Hungry Cowboys 38 Kritz 750
1101 The Red Door 58 Prinzen 10000
1102 Crying Hats 14 Cox 10000
1103 Trail End 1 Acconci 8000
1104 Untitled 60 null 1800
1105 Meteor Show 60 null 10000
1106 Horse Corral 40 Long 12500
1107 Striking It Rich 35 Irvin 1750
1108 Untitled Mural 60 null 400
1109 Friends 22 Fratt 16000
1110 Three Sisters 60 null 6500
1111 Untitled (man and crucifix) 60 null 3200
1112 Dark Canyon 27 Gonzales 8000
1113 Shadow House 50 Mogan 5500
1114 Storytelling at the Campfire 50 Mogan 18000
1115 Starry Night 25 Giama 8500
1116 Apache Warrior 30 Higgins 23000
The total number of the Art Works and Artists: 117 and the total appraised value: 1144623
As you can
Explanation / Answer
import java.io.*;
import java.util.*;
public class ArtistTest {
private String artistName; // name of artist
private int artistId; //id of artist
// constructor
ArtistTest(int number, String name) {
this.artistId = number;
this.artistName = name;
}
//return the artistName
public String getArtistName() {
return artistName;
}
//set the artistName
public void setArtistName(String artistName) {
this.artistName = artistName;
}
//return the artistId
public int getArtistId() {
return artistId;
}
// set the artistId
public void setArtistId(int artistId) {
this.artistId = artistId;
}
// toString method
public String toString() {
return this.artistId + " " + this.artistName;
}
// main method
public static void main(String[] args) throws IOException {
// Testing the program with the following sample data.
ArtistTest artist1 = new ArtistTest(1, "Acconci");
ArtistTest artist2 = new ArtistTest(2, "Ames");
ArtistTest artist3 = new ArtistTest(3, "Aserty");
ArtistTest artist4 = new ArtistTest(4, "Baron");
ArtistTest artist5 = new ArtistTest(5, "Battenberg");
System.out.println(artist1.toString());
System.out.println(artist2.toString());
System.out.println(artist3.toString());
System.out.println(artist4.toString());
System.out.println(artist5.toString());
// reading records from file and putting in to an Array
List<ArtistTest> artistList = null;
try {
artistList = new ArrayList<ArtistTest>();
ArtistsArray(artistList);
} catch (IOException ioe) {
System.out.println("Exception while reading from file!");
}
// Reading the second file and checks the artist
// id present in the second file & array
try {
populateFile(artistList);
} catch (IOException ioe) {
System.out.println("Exception while reading from file!");
}
} // end main
/**
* Method to create output file by checking the artistId present in the
* second file
*/
private static void populateFile(List<ArtistTest> artistList)
throws IOException {
// Getting the file to process
Scanner myP1ArtsFile = new Scanner(new FileReader(
"C:/Users/patel/Desktop/p1arts.txt"));
// Creating output file
File outPutfile = new File(
"C:/Users/patel/Desktop/p1artists_out1.txt");
FileWriter fstream = new FileWriter(outPutfile);
BufferedWriter outputWriter = new BufferedWriter(fstream);
String artId = null;
String title = null;
String artistName = null;
int artistId = 0;
String apprisedValue = null;
outputWriter.write("ArtID Title ArtistID ArtistName Appraised Value ");
outputWriter.newLine();
int numberOfArtists = 1;
int totalValue = 0;
// Loop until the P1Arts file has next line
while (myP1ArtsFile.hasNext())
{
String string = null;
StringBuffer sb = new StringBuffer();
// ArtId
artId = myP1ArtsFile.next();
// loops until the ArtTitle is read
while (null != (string = myP1ArtsFile.next()))
{
if (!string.matches("^[\d ]*$"))
{
sb.append(string + " ");
}
else
{
// ArtTitle appending
title = sb.toString() + " ";
boolean artistFound = false;
// Checking weather the artist is present in the p1arts file
for (ArtistTest artist : artistList) {
artistId = artist.getArtistId();
int id = Integer.parseInt(string);
if (artistId == id) {
artistName = artist.getArtistName();
artistFound = true;
break;
}
}
if (!artistFound) {
System.out.println("Artist id " + string
+ " not present in the P1Arts file.");
artistFound = true;
}
// Appraised Value
apprisedValue = myP1ArtsFile.next();
totalValue += Integer.valueOf(apprisedValue);
String titlefinal = null;
String artistfinal = null;
// Padding spaces for columns in output file
if (title.length() < 70)
{
titlefinal = String.format("%-30s", title);
}
// Padding spaces in output file
if (null != artistName && artistName.length() < 30) {
artistfinal = String.format("%-10s", artistName);
}
// Writing the information into the output file
outputWriter.write(artId + " " + titlefinal + " "
+ artistId + " " + artistfinal + " "
+ apprisedValue);
artistName = null;
break;
}
}
// New line in output file
outputWriter.newLine();
numberOfArtists++;
}
outputWriter.newLine();
outputWriter.newLine();
outputWriter.newLine();
outputWriter.write("The total number of the Art Works and Artists: " + numberOfArtists
+ " and the total appraised value: " + totalValue);
outputWriter.flush(); // Clearing the writer
outputWriter.close(); // Closing the writer
myP1ArtsFile.close(); // Closing the file
}
/**
* This method is used to read the contents from p1artist file and populate
* array object
*/
private static void ArtistsArray(List<ArtistTest> artistList)
throws IOException
{
// Get the file
Scanner myfile = new Scanner(new FileReader(
"C:/Users/patel/Desktop/p1artists.txt"));
// boolean for line number and error scenario
int lineNumber = 1;
boolean error = false;
// Out file creation
File outPutfile = new File(
"C:/Users/patel/Desktop/p1artists_out1.txt");
// Creating a file stream to accept stream data
FileWriter fstream = new FileWriter(outPutfile);
// Writer to write the data to file
BufferedWriter outputWriter = new BufferedWriter(fstream);
String artistName = null;
int artistId = 0;
// loops until the file has next line
while (myfile.hasNext())
{
try
{
artistId = Integer.parseInt(myfile.next());
} catch (NumberFormatException nfe) {
error = true;
}
try {
if (null != (artistName = myfile.nextLine()))
artistList.add(new ArtistTest(artistId, artistName));
outputWriter.write(artistId + " " + artistName);
outputWriter.newLine();
} catch (Exception e) {
System.out.println("End of line Missing.");
}
// Numerical Error handling
if (error) {
System.out.println("Line Number : " + lineNumber
+ " Artist Id: " + artistId + " ArtistName: "
+ artistName);
System.out.println("Artist id should be in numeric format only.");
error = false;
}
lineNumber++;
}
myfile.close();// Closing the file
outputWriter.flush();// Clearing the writer
outputWriter.close();// Closing the writer
}
}