So I need to make this program that Reads a file with a scanner, inserts each key and value into a hashmap, and when the user types in one of those keys, it will print both the key and the value. If it has no value, it says so, and press "q" to quite. Here is my code:
import java.util.*;
import java.io.*;
public class Homework11 {
public static void main(String[] args) throws FileNotFoundException {
new File(System.getProperty("user.dir") + "/keyvalues.txt");
Scanner keyvalues = new Scanner(new File("keyvalues.txt"));
Scanner console = new Scanner(System.in);
Map<String, String> getvalues = new HashMap<String, String>();
while(keyvalues.hasNext()){
String key = keyvalues.next();
int number = keyvalues.nextInt();
String value = Integer.toString(number);
getvalues.put(key, value);
do {
System.out.println("Key: ");
String word = console.next();
if (!word.equals("q")){
if(word.contains(key)){
System.out.println("Key" + word + "has value" + value );
}
else {
System.out.println("key does not exist.");
}
}
}while (!console.equals("q"));
}
}
}
I am getting these errors when I run:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Homework11.main(Homework11.java:17)
I know it has something to do with my first while statement, but I'm not sure how to fix it. Any Ideas?