My function takes in a Scanner and returns a TreeMap map with Strings as keys, and an ArrayList as a value. These ArrayLists can be of different lengths. I've parsed the Scanner to a new ArrayList textData for easier iteration. The procedure should go as follows:
- If the element is a String, have that be the new key to the next TreeMap entry (stored as
String state) and clear the temporary array - If the element is parseable to a Double, add it to the temporary ArrayList
statePopData. - Commit to
map.
Ideally, with the scanner :
"Utah\t6.0\t60\n"
+ "California\t5\t30\n"
+ "Nevada\t3"
should return:
{"Utah",[6.0, 60.0], "California",[5.0, 30.0], "Nevada",[3.0],}
Here is what I have thus far:
public static TreeMap<String, ArrayList<Double>> readTable (Scanner dataSource)
{
//Parse Scanner to ArrayList
ArrayList<String> textData = new ArrayList<String>();
while(dataSource.hasNext()){
textData.add(dataSource.next());
}
//Populate TreeMap
ArrayList<Double> statePopData = new ArrayList<>();
TreeMap<String, ArrayList<Double>> map = new TreeMap<>();
for (int i = 0; i < textData.size(); i++) {
boolean isDouble;
String state = "";
try {
Double.parseDouble(textData.get(i));
isDouble = true;
} catch (NumberFormatException | NullPointerException nfe) {
isDouble = false;
}
if(isDouble) {
statePopData.add(Double.parseDouble(textData.get(i)));
} else { //means its a string
statePopData.clear();
state = textData.get(i);
}
if (statePopData.isEmpty()) {
map.put(state, statePopData);
}
} return map;}
I feel confident with the pieces, but I can never seem to run the map.put() statement at the right time with the proper values to commit. For example, my current results of the program are: {California=[3.0], Nevada=[3.0], Utah=[3.0]}
EDIT: The linked answer doesn't involve any implementation nor fully answers what I'm trying to do.