I have a text file data.txt. I want to input the data into a Hashmap and do some datamapping. When ever I hit the value without dot(). I will get an error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
How to I overcome it by skipping those entry without dot(.).
I created a small snippet to illustrate my problem.
static HashMap<String, String> newList = new HashMap<>();
public static void main(String[] args) throws FileNotFoundException, IOException {
String inputFile = "data.txt";
BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
String line;
while ((line = brInput.readLine()) != null) {
newList.put(line, "x");
}
for (Map.Entry<String, String> entry : newList.entrySet()) {
String getAfterDot = entry.getKey();
String[] split = getAfterDot.split("\\.");
String beforeDot = "";
beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
System.out.println(beforeDot);
}
}
data.txt
0
0.1
0.2
0.3.5.6
0.2.1
2.2
expected result when i print the map(doesnt need to be in order)
0
0
0.3.5
0.2
2