-1

I have a text file that I'm reading from and I'm trying to use the words in the text as file as keys in a hashmap. I then want to print the hashmap to see it's contents. This is a prelude to a larger project. I know I could simply print out the text file, but I'm trying to experiment with the hashmap data structure. Here's the code I have so far:

import java.io.*;  //needed for File class below
import java.util.*;  //needed for Scanner class below

public class readIn {

    public static void readInWords(String fileName){
        try{
            //open up the file
            Scanner input = new Scanner(new File(fileName));
            HashMap hm = new HashMap();
            while(input.hasNext()){
                //read in 1 word at a time and increment our count
                String x = input.next();
                System.out.println(x);
                hm.put(x);
            }
            System.out.println(hm);
        }catch(Exception e){
            System.out.println("Something went really wrong...");
        }   
    }


        public static void main(String args[]){
        int x = 10;  //can read in from user or simply set here

        String fileName = "test.txt";
        readInWords(fileName);
    }
}

When I run, I get a "no suitable method found for put(string)" error. My larger goal is to create this hash map and store a list of places where a certain keys appears as a value. However right now I'm just trying to learn more about hashmaps through practice and wanted to see if anyone knows why this doesn't work.

ben890
  • 1,068
  • 2
  • 14
  • 37
  • What problems are you having with this code?? – Hovercraft Full Of Eels Sep 24 '16 at 19:58
  • And what's your specific question? – Hovercraft Full Of Eels Sep 24 '16 at 19:58
  • Are you sure test.txt is exist or in right place? – Mirjalol Bahodirov Sep 24 '16 at 20:01
  • 2
    First, you need to read the file line by line, http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java, Second, split each line by space, so that you get words, Third, HashMap expects a key and value, in your case, if you don't want to put any value, either put a dummy object (new Object()) or use HashSet. – RP- Sep 24 '16 at 20:02
  • Yeah it's definitely in the right place. it's in the same directory. When I put a line in my while loop like System.println(x) (refering to the string I defined). The code compiles and runs perfectly. – ben890 Sep 24 '16 at 20:02
  • What do you think "no suitable method found for put(string)" means? – Tunaki Sep 24 '16 at 20:04
  • You should look at the Java reference, so you know what functions you can use. https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html – MyLone Trojan Sep 24 '16 at 20:04
  • RP: Is there a split function similar to the python split function? – ben890 Sep 24 '16 at 20:04
  • Tunaki: I'm guessing it means I can't use a string in my hashmap. However I'm seen numerous examples online where people have succesfully done it. – ben890 Sep 24 '16 at 20:05
  • Why do you think that? The error tells you that there is no suitable _method_. So the method `put(String)` doesn't exist. And if you look [at the documentation](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html), indeed, it doesn't exist. Because it is `put(key, value)`. – Tunaki Sep 24 '16 at 20:06
  • Tunaki: Sorry I literally just started learning java yesterday and I'm relatively new to coding in general. So there's no suitable method for storing a string as a key in a hashmap? – ben890 Sep 24 '16 at 20:09
  • The code compiles perfectly? With your put method? – Hovercraft Full Of Eels Sep 24 '16 at 20:18

1 Answers1

0

There are no .put(x) method in Map iterface. You must use put() method with key argument. In your situation do it like this: hm.put(x, x);

  • I got an error that says readIn.java users unchecked or unsafe operations. and that I should recompile with -Xlint:unchecked for details. What exactly does this mean? What was unsafe? – ben890 Sep 24 '16 at 20:10
  • HashMap type is unsafe. Change to HashMap hm = new HashMap<>(); – Mirjalol Bahodirov Sep 24 '16 at 20:13
  • this works, thanks! Is there an easy to initialize the value of my hashmap as an array? – ben890 Sep 24 '16 at 20:22
  • Nope, there are no such way. You need to init like above. If it is not compatible to your problem. Then you can use Vector, List or ... – Mirjalol Bahodirov Sep 24 '16 at 20:33