tags:

views:

445

answers:

3

Hi

I can't find a solution on this by myself. Please give me a tip or something

import java.util.*;


       import java.io.*;

        class Fulgleinfluens {
        public static void main(String[] args) {

        HashMap <String, Komm> Komm = new hashMap<String, Komm>();

        int teller = 0;

         try {
         Scanner FilKom = new Scanner(new File("KommuneKoordinater.txt"));

         while(FilKom.hasNextLine()) {
      String linje = FilKom.nextLine();
      String [] dellinje = linje.split(",");
      String kommune = dellinje[0];
      String fylke = dellinje[1];
      String lengdegrad = dellinje[2];
      String breddegrad = dellinje[3];

      Komm enKom = new Komm(kommune, fylke, lengdegrad, breddegrad);
      Komm.put(kommune, enKom);
      teller++;

         }
     } catch (Exception e) {
         System.out.println("En feil oppsto ved lesing av fil");
     }
     System.out.println("Lest "+teller+" antall linjer");
        }

        void getKommuneMap () {
     String kommune = "Sarpsborg";
     Komm enKom = (Komm) Komm.get(kommune);
        }
        }

        class Komm {
        String kommune;
        String fylke;
        String lengdegrad;
        String breddegrad;

        Komm(String kommune, String fylke, String lengdegrad, String breddegrad) {
             this.kommune = kommune;
             this.fylke = fylke;
             this.lengdegrad = lengdegrad;
             this.breddegrad = breddegrad;
        }
        }
A: 

Are your imports correct? Try :

import java.util.*;  // or java.util.HashMap 
import java.io.*;

I see the imports corrected. But your code is messed up. Your Komm variable is defined so many times.

fastcodejava
A: 

You're trying to call Komm.get() which will only work if get() is defined as a static method in the class Komm. As far as I can tell, it's not.

MatrixFrog
+1  A: 

You can't name an instance of your map the same as the name of the class Komm. Change it to komm and it should be ok. Right now you are trying to call a static method on Komm rather than HashMap.get().

danben
`komm` is a terrible name for anything that's not an instance of `Komm`. Maybe call it `kommMap` or something. I don't know what Komm means so I'm not sure what you're trying to do here. But I really don't think a variable should be named `komm` if it's not referring to a `Komm`.
MatrixFrog
Thanks for the quick response! I'm new to programming(java) and learning, thanks for alle the answares. When changing the HashMap name to komm, the compilator cant find variabel komm in Komm enKom = (Komm) komm.get(kommune);
@MatrixFrog - you're right, I got confused between the types. `kommMap` is a better choice.
danben
@cbeberge - how did you change the name? Please post your updated code.
danben