Hi basically wrote this code, which stores a hashmap into an external text file So when the user types in write, the first value they type after write (index 1), and the second value at index(2), gets stored as a key and value respectively. The problem is that it all has to be on one line and if the user presses return after they type in "write" the system crashes. So how can I make it so that it doesn't allow the user to press return until they have typed a key and value?
This is my code,
if (input.contains("write")) {
String key = input.get(1);
String value = "";
for(int i=2; i<input.size(); i++) {
value = value + " " + input.get(i);
}
instruct.mapWrite(key, value);
}
I use a scanner class to get input then use the following method to split the string into an Array. This is how I can store the input into a HashMap.
public ArrayList<String> getInput()
{
System.out.print("> "); // print prompt
String inputLine = reader.nextLine().trim().toLowerCase();
String[] wordArray = inputLine.split(" "); // split at spaces
// add words from array into ArrayList
ArrayList<String> words = new ArrayList<String>();
for(String word : wordArray) {
words.add(word);
}
return words;
}
You cannot prevent it at this level.
But
if(wordArray.length < 3)
), print error message and ask again.