I’m trying to assign numbers from text file to hashtable first number as key and second number as value and text file looks like first line (123 321)second line(456 565) n.th line(789 875) so i’m trying to assign the number after space as a value.
I can’t figure out how to do it tried with
Scanner scanner = new Scanner(new File("C:/Users/msi/Documents/number.txt"));
scanner.useDelimiter(" ");
Hashtable<String, String> numbers = new Hashtable<String, String>();
while(scanner.hasNext())
{
numbers.put(scanner.next(),scanner.next());
System.out.println(numbers);
}
As i unterstand it the Scanner class is for parsing files linewise. By setting the delimiter to the space character ” ” you get the error that the next newline isn’t a delimiter anmore. Therefore you would get the following values
A way to solve this is to split the values after they have been read linewise: