2017-02-26 75 views
-3
package BIB; 
    import java.util.*; 

    public class Mapping_File{ 

      static Map<String, String> words = new HashMap<>(); 

      public static void main(String[] args){ 

        words.put("WCRE", "Working Conference on Reverse Engineering (WCRE"); 
        words.put("ICSE", "International Conference on Software Engineering (ICSE)"); 
        words.put("IWSC", "International Workshop on Software Clones (IWSC)"); 
        words.put("ASE", "Automated Software Engineering (ASE)"); 
        words.put("ACSAC", "Annual Computer Security Applications Conference (ACSAC)"); 


      }// end of main method 

      public Map <String, String> getWordsMap(){ 

        return words; 

      }// end of the get method 
    }// end of the class 

这是我的类,其中包含哈希映射,我相信,它工作得很好。现在我有另一个应该从这个映射文件访问值的类。 package BIB;我做了一个类来创建一个HashMap,我无法实现它在java中的相同包中构建另一个类

import java.io.*; 
    import java.util.*; 
    import java.lang.*; 

    public class FileParser{ 

      Mapping_File mf = new Mapping_File(); 

      public static void main(String[] args){ 

        String Readline = "WCRE"; 
        String converted = null; 
        Map<String, String> word = mf.getWordsMap(); 

        if(word.containsKey(Readline)){ 

          converted = word.get(Readline); 

        }// end of the if 

        else if(word.isEmpty()){ 

          converted = "the map you are looking for is empty."; 


        }// end of else if 

        else{ 

          converted = "Could not find the elaborated form"; 

        }// end of else 

      System.out.println(converted); 

      }// end of the main method 


    }// end of the class FileParser 

我一直在得到的是“你要找的地图是空的。”我在尝试访问映射文件时是否犯了错误?

+1

这不应该编译。可以? –

+1

你有两种不同的MappingFile和Mapping_File的拼写,你在一个静态方法中使用非静态字段....再次编译? –

+0

只有一个'main'方法会执行 - 无论您在启动Java时指定哪一个方法。如果您使用'FileParser.main'启动,则'MappingFile.main'不会被执行。也许你想在'MappingFile'中使用静态类初始化器而不是第二种'main'方法? –

回答

0
  1. 您的代码错误,当我尝试重新编译它时出现语法错误(将MappingFile重命名为Mapping_File)。
  2. 取而代之的是声明main,声明你的地图构造函数

改变你的映射类成为下面的代码:

公共类Mapping_File {

 static Map<String, String> words = new HashMap<>(); 

    public Mapping_File(){ 

      words.put("WCRE", "Working Conference on Reverse Engineering (WCRE"); 
      words.put("ICSE", "International Conference on Software Engineering (ICSE)"); 
      words.put("IWSC", "International Workshop on Software Clones (IWSC)"); 
      words.put("ASE", "Automated Software Engineering (ASE)"); 
      words.put("ACSAC", "Annual Computer Security Applications Conference (ACSAC)"); 


    }// end of main method 

    public Map <String, String> getWordsMap(){ 

      return words; 

    }// end of the get method 
}// end of the class 
+0

在构造函数中声明我的地图非常感谢你 – Reecha

相关问题