failing in inserting values in maps java

百般思念 提交于 2020-01-07 05:11:05

问题


I am trying to insert values into maps in java to loop on them and do some calculations so what I am doing is to read a folder of 11k files which containing somethings like these: ps: all the files have the same structure

 SFrm  EFrm   SegAScr Phone
        0    36   -158051 SIL
       37   105   -644247 +NONTRANS+
      106   109    -96452 l SIL w b
      110   112   -125055 w l aa i
      113   115   -150550 aa w 7 i
      116   118   -146662 7 aa i i
      119   122    -46757 i 7 d i
      123   126    -58440 d i SIL e
      127   146    -90776 +MUSIC+
      147   152    -61098 t SIL u b
      153   158    -67393 u t f i
      159   174   -251284 f u f i
      175   178    -79772 f f aa i
      179   194   -134562 aa f 7 i
      195   206    -33695 7 aa a i
      207   223   -194024 a 7 SIL e
      224   350   -434997 +NOISE+
      351   353    -28280 SIL
 Total score:    -2802095

and checking on them by the code below

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class z {
//Enum to match the vowels with string values
    public enum Match {
        _aa("aa"), _ii("ii"), _uu("uu");
        public String value;

        private Match(String value) {
            this.value = value;
        }

        public static Match fromString(String s) {
            for (Match m : Match.values()) {
                if (s.substring(4, 6).equals(m.value))
                    return m;
            }

            return null;
        }
    }

    public static void main(String[] args) throws NumberFormatException,
            IOException {

        **//reading folder**
        File folderPhseg = new File(
                "/home/bassem/workspace/outputs/new");
        File[] listOfPhseg = folderPhseg.listFiles();

        Map<Match, List<Integer>> indexes = new HashMap<Match, List<Integer>>();
         for (Match m : Match.values()) {
                    indexes.put(m, new ArrayList<Integer>());
                }
        String line = "";
        for (File file : listOfPhseg) {
            if (file.isFile()) {

                FileReader inputFile = new FileReader(file.getAbsolutePath());
                BufferedReader bufferReader = new BufferedReader(inputFile);
                String[] column1 = new String[100];
                String[] column2 = new String[100];
                String[] column3 = new String[100];
                String[] column4 = new String[100];
                String[] column5 = new String[100];
                int index = 0;
                  //converting it into five arrays
                while ((line = bufferReader.readLine()) != null) {

                    String temp = "";
                    int count = 1;
                    column4[index] = "";
                    // System.out.println(line);
                    StringTokenizer st = new StringTokenizer(line, " ");

                    // String tokenizer gets the token from each space
                    while (st.hasMoreTokens()) {

                        temp = st.nextToken();
                        if (temp.equals("Total")) {
                            break;
                        }
                        // parsing input
                        if (count == 1) {
                            column1[index] = temp;
                        }
                        if (count == 2) {
                            column2[index] = temp;
                        }
                        if (count == 3) {
                            column3[index] = temp;
                        }
                        if (count == 4) {
                            column4[index] = temp;
                        }
                        if (count == 5) {
                            column5[index] += temp;
                        }

                        if (count < 5)
                            count++;
                    }

                }
                    //storing them into Maps

                for (int k = 0; k < index - 1; k++) {
                    String cur = column5[k];
                    Match m = Match.fromString(cur);
                    if (m != null) {
                        indexes.get(m).add(
                                Integer.valueOf(column3[k])
                                        - Integer.valueOf(column2[k]));
                    }
                }

                index++;

            }


        }
        System.out.println(indexes);

    }
}

I store these data into the map with key Match then when I try to print the map I get an empty one!

I tried this code on one of the files and it worked well with me. the problem is when I try to apply it on the whole folder I get nothing in the map. After debugging the code I figured out that the last for loop isn't getting executed and this loop is the one concerned with storing in map but I can't figure out the reason behind that. I tried to move index++; to be above the for loops. The code Entered the mentioned for loop but at the end I got the same output


回答1:


Well, you're doing a nasty overwrite here - in each iteration you lose all the values and you start counting from zero...

//storing them into Maps
for (Match m : Match.values()) {
    indexes.put(m, new ArrayList<Integer>());
}

Also, after the last line you "break" out of the inner cycle, but the map will be overwritten with empty stuff anyway.

Move the above "for" block right after the part where you create the "indexes" map.



来源:https://stackoverflow.com/questions/36607917/failing-in-inserting-values-in-maps-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!