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?

Member Avatar for james.newell

Hi pn2babe.

The Java compiler is reporting your problem on line 17. What appears to be happening with your while loop is this:

15: Check there is input to scan
16: Scan the input as a string
17: Scan input as an integer (without first checking whether there is input to scan)

You need to add an if statement to test whether there is another input ready to be scanned and converted to an integer.

Hope that helps!

Ok, I got it to compile, but I still can't get it to do what I want correctly. I have tried a ton of different ways. I got it to print the values when a number 1-10 is imputed, but anything else will just return null when I want it to say "key does not exist".

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 input = new Scanner(System.in);
         Map<String, String> getvalues = new HashMap<String, String>();
        
         while(keyvalues.hasNext()){
            String key = keyvalues.next();
            String value = keyvalues.next();
            getvalues.put(key, value);
         }

            do {
               System.out.println("Key: ");
               String word = input.next();
               String getword = getvalues.get(word);
               if ((!input.equals("q"))){
                  System.out.println("Key: " + word + " has value: " + getword);
               }
               else {
                  System.out.println("key does not exist.");
               }
            }while (!input.equals("q"));          
      }	
   }

I tried: containsvalue = getvalues.containsValue(word)
at the beginning of the if statement - if(getvalues.containsValue(word)){
but then anything that I input would say "Key does not exist". I somehow need to get the values and check if the input contains one of them...
Also my 'press "q" for quit doesn't work and I have no idea why..

Member Avatar for james.newell

Try something like this:

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 = 0;
if (keyvalues.hasNext()) {
            number = keyvalues.nextInt();
            String value = Integer.toString(number);
            getvalues.put(key, value);
} else {
//do something here.... Show message and exit?
}
            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"));         
         }
      }	
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.