Reputation:
I'm creating a ChatBox program that responds to keywords being typed into my Java program. I have a list of responses stored as a HashMap, in an external txt file. I want to create a method that enables me to add to a HashMap new keysword(key):responses(values) and therefore, add to the external txt file.
I already have key: values inserted into this text file, but I'm having difficult trying to add new keys: values to it, in the correct format, and then retrieving them.
I have a method called 'addtoresponses()' which should take the string parameters from the method and insert them into a hashMap. The hashMap will then be written to a txt file using another method, from another class called 'listMap'.
When I run my code, the hashMap does write to the external text file, so that seems to work (it writes it to the external text file, so it can be used by another method later on). However, when I run my program again, I instantly get an error message stating that;
Missing response for Hello in file missing-map.txt (hello being the key inserted).
It appears that the key:value I'm adding in, is the losing the 'value' part of whatever I'm typing into my method. I've been told that it's possibly my format i.e. it should be stored in the following way;
**key
response all on one line of length
key
another response all on one line
etc.**
But I'm unclear in how I can achieve this. I'm pretty sure it's not the method that's inserting the HashMap into the txt file. I believe it's how I'm putting the values into the HashMap but I'm not greatly confident.
Would greatly appreciate it if someone could look over my code and try and see where I'm going wrong, in relation to why this error message is appearing and how I can fix it.
I've posted the code in below;
Constructor for InstructorMode >>
public InstructorMode()
{
helper = new FileAssistance();
edits = new HashMap<String, String>();
}
Method for adding to HashMap (in InstructorMode class) >>
public void addtoresponse(String key, String value)
{
edits.put(key, value);
helper.listaMap(edits, "missing-map.txt");
}
Method for adding to external txt file (class called FileAssistance) >>
public void listaMap(HashMap<String, String> map, String filename)
{
if(map != null) {
try (FileWriter writer = new FileWriter(filename, true)) {
for(String key : map.keySet()) {
String value = map.get(key);
if(value == null) {
System.out.println("Warning: " +
key + " in listaMap.");
value = "Not sure";
}
writer.write(key.trim());
writer.write('\n');
writer.write(value.trim());
writer.write('\n');
}
writer.close();
}
catch(IOException e) {
System.out.println("Issue: " + filename +
" in listaMap");
}
}
else {
System.out.println("Null map passed to listaMap.");
}
}
Would appreciate any help on this!
Edit - code which reads a HashMap >>
public HashMap<String, String> lookatM(String filename)
{
HashMap<String, String> map = new HashMap<>();
try (BufferedReader reader =
new BufferedReader(new FileReader(filename))) {
String word;
word = reader.readLine();
while(word != null) {
String response = reader.readLine();
if(response != null) {
response = response.trim();
if(response.length() != 0) {
map.put(word, response);
}
else {
System.out.println("Blank response for " +
word + " in file " +
filename);
}
}
else {
System.out.println("Missing response for " +
word + " in file " +
filename);
}
word = reader.readLine();
}
}
catch(IOException e) {
System.out.println("Problem reading file: " + filename +
" in LookatM");
}
return map;
}
Upvotes: 0
Views: 3573
Reputation: 563
I haven't tried running your code on my machine but here are few suggestions just by looking at your snippets -
1. Please check for any hidden characters in your file containing key-value rows. One more thing to check if you have empty newline at the end of a file.
2. One more thing to check if you are getting this error for just last key-value pair in your file.
3. I know this is obvious but debug in IDE can be helpful too!!
One last thing, if you have to store map into file and reconstruct from file then you can serialize map object into file and reconstruct object from that serialized file. Here is simple tutorial tutorial I found online.
properties file also do that job as mentioned above.
Upvotes: 0
Reputation: 56
You can use a java.lang.Properties
which is basically an HashMap
that can interact with a .properties
file.
A properties file, in turn, is nothing more than a list of key and values separated by '='
For example, you can have a property file with the following content:
Name=Sauron
Attitude=Nice in a peculiar way
[...]
You can load a property file in a Properties
object using Properties.load()
If you want to add more key/value pairs just use the method Properties.setProperty()
If you want to save the state of your Properties object to the backing properties file, just use Properties.store
For more information, see the Properties javadoc
Additionally, here you can find a tutorial on how to use the Properties in your code
Upvotes: 2
Reputation: 133
You are doing reader.readLine
twice in loop and thats the reason of your output message. You just should use 'standard'
while((word = reader.readLine()) != null){
//loop body
}
Upvotes: 0