Null Output generated when using BufferedReader/PrintWriter
It would also appear that I am not able to read the file either.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package daos;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.City;
import model.YearData;
import repositories.Repository;
/**
*
* @author mga
*/
public class DAOTextImpl implements DAOInterface {
static final char DELIMITER = ',';
ArrayList yeardata=new ArrayList<>();
@Override
public Repository load(String filename) {
Repository repository = new Repository();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String temp;
String line = br.readLine();
while (line != null) {
temp = line.split(Character.toString(DELIMITER));
int id = Integer.parseInt(temp[0]);
String cityName = stripQuotes(temp[1]);
String country = stripQuotes(temp[2]);
int noOfLines = Integer.parseInt(temp[2]);
// for (int i = 0; i < noOfLines; i++)
// {
line = br.readLine();
temp = line.split(Character.toString(DELIMITER));
String year = stripQuotes(temp[0]);
float precipitation = Float.parseFloat(temp[1]);
int maxTemp = Integer.parseInt(temp[2]);
int minTemp = Integer.parseInt(temp[3]);
int windSpeed = Integer.parseInt(temp[4]);
String windDirection = stripQuotes(temp[5]);
yeardata.add(year);
yeardata.add(precipitation);
yeardata.add(maxTemp);
yeardata.add(minTemp);
yeardata.add(windSpeed);
yeardata.add(windDirection);
City city=new City(id,cityName,country,yeardata);
// }
repository.add(city);
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return repository;
}
@Override
public void store(String filename, Repository repository) {
try (PrintWriter output = new PrintWriter(filename)) {
output.print(repository.toString());
output.print(repository);
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String stripQuotes(String str) {
return str.substring(1, str.length() - 1);
}
}
My Repository class is as follows:
package repositories;
import daos.DAOTextImpl;
import java.util.ArrayList;
import java.util.function.Predicate;
import model.City;
import model.YearData;
public class Repository implements RepositoryInterface {
private ArrayList<City> items;
static char DELIMITER=',';
public Repository() {
this.items = new ArrayList<>();
}
public Repository(ArrayList <City> items) {
this.items = items;
}
public Repository(String filename) {
this();
// Create dao and execute load
DAOTextImpl dao = new DAOTextImpl();
}
@Override
public ArrayList<City> getItems() {
return this.items;
}
@Override
public void setItems(ArrayList<City> items) {
this.items = items;
}
@Override
public void add(City items) {
this.items.add(items);
}
@Override
public void remove(int id) {
Predicate<City> predicate = e->e.getId() == id;
this.items.removeIf(predicate);
}
@Override
public City getItem(int id) {
for (City item:this.items) {
if (item.getId() == id)
return item;
}
return null;
}
@Override
public String toString() {
System.out.println(items);
return "nItems: " + this.items;
}
@Override
public void store(String filename) {
// create dao and execute store
DAOTextImpl dao = new DAOTextImpl();
dao.store(filename, this);
}
}
I have stress tested the program, inserting multiple output.print(repository.toString());
statements within it. For example, inserting 20 of the output.print(repository.toString());
statements will insert into the file generated, 20 items:
which would lead me to think that there is an error within the Repository class/interface, but I am for the life of me utterly unsure as to how/why this is being generated.
I am not sure if the fact that files do not seem to be properly read is associated with this issue at present, or if rather, one error is merely masking another.
By virtue of the fact that I am unsure as to what is causing the problem, I feel that I need to include the full length of the code, if only to facilitate the error-checking process.
java file repository
add a comment |
It would also appear that I am not able to read the file either.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package daos;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.City;
import model.YearData;
import repositories.Repository;
/**
*
* @author mga
*/
public class DAOTextImpl implements DAOInterface {
static final char DELIMITER = ',';
ArrayList yeardata=new ArrayList<>();
@Override
public Repository load(String filename) {
Repository repository = new Repository();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String temp;
String line = br.readLine();
while (line != null) {
temp = line.split(Character.toString(DELIMITER));
int id = Integer.parseInt(temp[0]);
String cityName = stripQuotes(temp[1]);
String country = stripQuotes(temp[2]);
int noOfLines = Integer.parseInt(temp[2]);
// for (int i = 0; i < noOfLines; i++)
// {
line = br.readLine();
temp = line.split(Character.toString(DELIMITER));
String year = stripQuotes(temp[0]);
float precipitation = Float.parseFloat(temp[1]);
int maxTemp = Integer.parseInt(temp[2]);
int minTemp = Integer.parseInt(temp[3]);
int windSpeed = Integer.parseInt(temp[4]);
String windDirection = stripQuotes(temp[5]);
yeardata.add(year);
yeardata.add(precipitation);
yeardata.add(maxTemp);
yeardata.add(minTemp);
yeardata.add(windSpeed);
yeardata.add(windDirection);
City city=new City(id,cityName,country,yeardata);
// }
repository.add(city);
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return repository;
}
@Override
public void store(String filename, Repository repository) {
try (PrintWriter output = new PrintWriter(filename)) {
output.print(repository.toString());
output.print(repository);
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String stripQuotes(String str) {
return str.substring(1, str.length() - 1);
}
}
My Repository class is as follows:
package repositories;
import daos.DAOTextImpl;
import java.util.ArrayList;
import java.util.function.Predicate;
import model.City;
import model.YearData;
public class Repository implements RepositoryInterface {
private ArrayList<City> items;
static char DELIMITER=',';
public Repository() {
this.items = new ArrayList<>();
}
public Repository(ArrayList <City> items) {
this.items = items;
}
public Repository(String filename) {
this();
// Create dao and execute load
DAOTextImpl dao = new DAOTextImpl();
}
@Override
public ArrayList<City> getItems() {
return this.items;
}
@Override
public void setItems(ArrayList<City> items) {
this.items = items;
}
@Override
public void add(City items) {
this.items.add(items);
}
@Override
public void remove(int id) {
Predicate<City> predicate = e->e.getId() == id;
this.items.removeIf(predicate);
}
@Override
public City getItem(int id) {
for (City item:this.items) {
if (item.getId() == id)
return item;
}
return null;
}
@Override
public String toString() {
System.out.println(items);
return "nItems: " + this.items;
}
@Override
public void store(String filename) {
// create dao and execute store
DAOTextImpl dao = new DAOTextImpl();
dao.store(filename, this);
}
}
I have stress tested the program, inserting multiple output.print(repository.toString());
statements within it. For example, inserting 20 of the output.print(repository.toString());
statements will insert into the file generated, 20 items:
which would lead me to think that there is an error within the Repository class/interface, but I am for the life of me utterly unsure as to how/why this is being generated.
I am not sure if the fact that files do not seem to be properly read is associated with this issue at present, or if rather, one error is merely masking another.
By virtue of the fact that I am unsure as to what is causing the problem, I feel that I need to include the full length of the code, if only to facilitate the error-checking process.
java file repository
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 20 '18 at 5:47
add a comment |
It would also appear that I am not able to read the file either.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package daos;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.City;
import model.YearData;
import repositories.Repository;
/**
*
* @author mga
*/
public class DAOTextImpl implements DAOInterface {
static final char DELIMITER = ',';
ArrayList yeardata=new ArrayList<>();
@Override
public Repository load(String filename) {
Repository repository = new Repository();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String temp;
String line = br.readLine();
while (line != null) {
temp = line.split(Character.toString(DELIMITER));
int id = Integer.parseInt(temp[0]);
String cityName = stripQuotes(temp[1]);
String country = stripQuotes(temp[2]);
int noOfLines = Integer.parseInt(temp[2]);
// for (int i = 0; i < noOfLines; i++)
// {
line = br.readLine();
temp = line.split(Character.toString(DELIMITER));
String year = stripQuotes(temp[0]);
float precipitation = Float.parseFloat(temp[1]);
int maxTemp = Integer.parseInt(temp[2]);
int minTemp = Integer.parseInt(temp[3]);
int windSpeed = Integer.parseInt(temp[4]);
String windDirection = stripQuotes(temp[5]);
yeardata.add(year);
yeardata.add(precipitation);
yeardata.add(maxTemp);
yeardata.add(minTemp);
yeardata.add(windSpeed);
yeardata.add(windDirection);
City city=new City(id,cityName,country,yeardata);
// }
repository.add(city);
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return repository;
}
@Override
public void store(String filename, Repository repository) {
try (PrintWriter output = new PrintWriter(filename)) {
output.print(repository.toString());
output.print(repository);
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String stripQuotes(String str) {
return str.substring(1, str.length() - 1);
}
}
My Repository class is as follows:
package repositories;
import daos.DAOTextImpl;
import java.util.ArrayList;
import java.util.function.Predicate;
import model.City;
import model.YearData;
public class Repository implements RepositoryInterface {
private ArrayList<City> items;
static char DELIMITER=',';
public Repository() {
this.items = new ArrayList<>();
}
public Repository(ArrayList <City> items) {
this.items = items;
}
public Repository(String filename) {
this();
// Create dao and execute load
DAOTextImpl dao = new DAOTextImpl();
}
@Override
public ArrayList<City> getItems() {
return this.items;
}
@Override
public void setItems(ArrayList<City> items) {
this.items = items;
}
@Override
public void add(City items) {
this.items.add(items);
}
@Override
public void remove(int id) {
Predicate<City> predicate = e->e.getId() == id;
this.items.removeIf(predicate);
}
@Override
public City getItem(int id) {
for (City item:this.items) {
if (item.getId() == id)
return item;
}
return null;
}
@Override
public String toString() {
System.out.println(items);
return "nItems: " + this.items;
}
@Override
public void store(String filename) {
// create dao and execute store
DAOTextImpl dao = new DAOTextImpl();
dao.store(filename, this);
}
}
I have stress tested the program, inserting multiple output.print(repository.toString());
statements within it. For example, inserting 20 of the output.print(repository.toString());
statements will insert into the file generated, 20 items:
which would lead me to think that there is an error within the Repository class/interface, but I am for the life of me utterly unsure as to how/why this is being generated.
I am not sure if the fact that files do not seem to be properly read is associated with this issue at present, or if rather, one error is merely masking another.
By virtue of the fact that I am unsure as to what is causing the problem, I feel that I need to include the full length of the code, if only to facilitate the error-checking process.
java file repository
It would also appear that I am not able to read the file either.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package daos;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.City;
import model.YearData;
import repositories.Repository;
/**
*
* @author mga
*/
public class DAOTextImpl implements DAOInterface {
static final char DELIMITER = ',';
ArrayList yeardata=new ArrayList<>();
@Override
public Repository load(String filename) {
Repository repository = new Repository();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String temp;
String line = br.readLine();
while (line != null) {
temp = line.split(Character.toString(DELIMITER));
int id = Integer.parseInt(temp[0]);
String cityName = stripQuotes(temp[1]);
String country = stripQuotes(temp[2]);
int noOfLines = Integer.parseInt(temp[2]);
// for (int i = 0; i < noOfLines; i++)
// {
line = br.readLine();
temp = line.split(Character.toString(DELIMITER));
String year = stripQuotes(temp[0]);
float precipitation = Float.parseFloat(temp[1]);
int maxTemp = Integer.parseInt(temp[2]);
int minTemp = Integer.parseInt(temp[3]);
int windSpeed = Integer.parseInt(temp[4]);
String windDirection = stripQuotes(temp[5]);
yeardata.add(year);
yeardata.add(precipitation);
yeardata.add(maxTemp);
yeardata.add(minTemp);
yeardata.add(windSpeed);
yeardata.add(windDirection);
City city=new City(id,cityName,country,yeardata);
// }
repository.add(city);
line = br.readLine();
}
br.close();
} catch (IOException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return repository;
}
@Override
public void store(String filename, Repository repository) {
try (PrintWriter output = new PrintWriter(filename)) {
output.print(repository.toString());
output.print(repository);
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String stripQuotes(String str) {
return str.substring(1, str.length() - 1);
}
}
My Repository class is as follows:
package repositories;
import daos.DAOTextImpl;
import java.util.ArrayList;
import java.util.function.Predicate;
import model.City;
import model.YearData;
public class Repository implements RepositoryInterface {
private ArrayList<City> items;
static char DELIMITER=',';
public Repository() {
this.items = new ArrayList<>();
}
public Repository(ArrayList <City> items) {
this.items = items;
}
public Repository(String filename) {
this();
// Create dao and execute load
DAOTextImpl dao = new DAOTextImpl();
}
@Override
public ArrayList<City> getItems() {
return this.items;
}
@Override
public void setItems(ArrayList<City> items) {
this.items = items;
}
@Override
public void add(City items) {
this.items.add(items);
}
@Override
public void remove(int id) {
Predicate<City> predicate = e->e.getId() == id;
this.items.removeIf(predicate);
}
@Override
public City getItem(int id) {
for (City item:this.items) {
if (item.getId() == id)
return item;
}
return null;
}
@Override
public String toString() {
System.out.println(items);
return "nItems: " + this.items;
}
@Override
public void store(String filename) {
// create dao and execute store
DAOTextImpl dao = new DAOTextImpl();
dao.store(filename, this);
}
}
I have stress tested the program, inserting multiple output.print(repository.toString());
statements within it. For example, inserting 20 of the output.print(repository.toString());
statements will insert into the file generated, 20 items:
which would lead me to think that there is an error within the Repository class/interface, but I am for the life of me utterly unsure as to how/why this is being generated.
I am not sure if the fact that files do not seem to be properly read is associated with this issue at present, or if rather, one error is merely masking another.
By virtue of the fact that I am unsure as to what is causing the problem, I feel that I need to include the full length of the code, if only to facilitate the error-checking process.
java file repository
java file repository
edited Nov 20 '18 at 5:49
Jack White
asked Nov 20 '18 at 4:50
Jack WhiteJack White
63
63
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 20 '18 at 5:47
add a comment |
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 20 '18 at 5:47
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 20 '18 at 5:47
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 20 '18 at 5:47
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386414%2fnull-output-generated-when-using-bufferedreader-printwriter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386414%2fnull-output-generated-when-using-bufferedreader-printwriter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 20 '18 at 5:47