-
Java hashmap help
Hi guys I am trying to read a file of integers that is information about way points into a hashmap. This is what I have so far
import java.io.*;
import java.util.*;
import java.util.HashMap;
import java.util.Iterator;
class Waypoints {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader("waypoints.txt"));
HashMap<String, String> map = new HashMap<String, String>();
while (scanner.hasNextInt()) {
HashMap.put(scanner.nextInt());
}
System.out.println(map);
}
}
when I try to compile I keep getting this error...
Test.java:16: cannot find symbol
symbol : method put(int)
location: class java.util.HashMap
HashMap.put(scanner.nextInt());
Please help me resolve this or point me in the right direction I'm not even sure if I am on the right way of doing this.
-Thanks
-
Re: Java hashmap help
Please use code tags when posting code.
You have created an instance of HashMap but are then using the classname when calling put(). This means you are trying to call a static method called put() which the HashMap class doesn't have, hence the warning.
Mind you there isn't an instance method called put() which only has one argument either. A mapping requires 2 arguments, the key and the value. Always read the Java Docs when using classes to make sure you using them correctly.
-
Re: Java hashmap help
Also try to understand the Error so that you can solve it by yourself in future.
Test.java:16: cannot find symbol ( this line says that problem is on line 16 on file Test.java)
Can not find symbol means either variable, method or class whichever is on that line is not exists, this may be due to typo or anything else.
these are the extra details
symbol : method put(int)
location: class java.util.HashMap
here symbol is method put which takes an int argument and as said by Keang that doesn't exists.