I have a text file that looks like following:
A
Apple
B
Bat
C
Cat
......
I need to read this text file and save it in a HashMap where an odd line is a key and the following even line is the value. For example, (A, Apple). I have tried with the following code, it doesn't work. Can someone give me a hint or advice on how I can do it?
private HashMap<String, String> newHashMap = new HashMap<String, String>();
Charset charset = Charset.forName("US-ASCII");
Path path = Paths.get("file_location");
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
int lineCount = 0;
String key;
String value;
String line = reader.readLine();
while(line != null) {
line = reader.readLine();
if (lineCount % 2 == 1)
{
key = reader.readLine() ;
}
else if( lines % 2 == 0)
{
value = reader.readLine();
}
lineCount++;
newHashMap.put(key, value);
}