我有一个字符串中的数字。如何将数字放到hashmap?

时间:2017-04-18 17:09:49

标签: java string hashmap

在这段代码中,我试图用hashmap.so创建kruskal算法代码的实现代码,在第一个代码中我将代码保存到字符串中,然后需要将字符串的值输入到hashmap中。

private static final String FILENAME = "D:\\Kuliah\\Semester 7\\~USUL BULAN JANUARI\\PROBLEM\\sepuluh\\1.dat";

    public static void main(String[] args) {

                String content = "";

        BufferedReader br = null;
        FileReader fr = null;

        try {

            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String sCurrentLine;

            br = new BufferedReader(new FileReader(FILENAME));

            while ((sCurrentLine = br.readLine()) != null) {
                            content = content + sCurrentLine + "\n";
    //          System.out.println(sCurrentLine);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

             System.out.print(content);

    }
         public static String extractNode(String content){
            String[] lines = content.split("\n");
            HashMap<Integer, Node> vertexs = new HashMap<Integer, Node>();

            for(int i=0;i<lines.length;i++){
             Node node = new Node();
             node.setId(0);
             vertexs.put(0, node);

                 //at this code how can i put it?

            }





            return "";
        }




}

1 个答案:

答案 0 :(得分:0)

例如,你可以实现这个:

String x = "5";
int num = Integer.parseInt(x); //coverts String to Integer

希望这有帮助!

`