无法在映射java中插入值 [英] failing in inserting values in maps java

查看:128
本文介绍了无法在映射java中插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在java中插入值到地图上来循环它们并做一些计算,所以我正在做的是读取一个包含如下所示的11k文件的文件夹:
ps:所有文件都有相同的结构

  SFrm EFrm SegAScr电话
0 36 -158051 SIL
37 105 -644247 + NONTRANS +
106 109 -96452 l SIL wb
110 112 -125055 wl aa i
113 115 -150550 aa w 7
116 118 -146662 7 aa ii
119 122 -46757 i 7 di
123 126 -58440 di SIL e
127 146 -90776 +音乐+
147 152 -61098 t SIL ub
153 158 -67393 utfi
159 174 -251284 fufi
175 178 -79772 ff aa i b $ b 179 194 -134562 aa f 7 i
195 206 -33695 7 aa ai
207 223 -194024 a 7 SIL e
224 350 -434997 + NOISE +
351 353 -28280 SIL
总分:-2802095

并通过下面的代码检查它们

  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用字符串值匹配元音
public enum匹配{
_aa(aa),_ii(ii ),_uu(uu);
public String value;

私人匹配(字符串值){
this.value = value;

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

返回null;



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

** //读取文件夹**
文件folderPhseg =新文件(
/ home / bassem / workspace / outputs / new);
File [] listOfPhseg = folderPhseg.listFiles();

地图<匹配,列表<整数>>索引=新的HashMap<匹配,列表<整数>>(); (匹配m:Match.values()){
indexes.put(m,new ArrayList< Integer>());

}
String line =; (文件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;
//将其转换为五个数组
while((line = bufferReader.readLine())!= null){

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

// String tokenizer从每个空间获取标记
while(st.hasMoreTokens()){

temp = st.nextToken();
if(temp.equals(Total)){
break;
}
//解析输入
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 ++;
}

}
//将它们存入地图

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

index ++;

}


}
System.out.println(indexes);


$ b $ / code $ / pre

我将这些数据存入地图与关键 Match 然后当我尝试打印地图我得到一个空的!



我试过这个代码其中一个文件,它与我很好。
问题是,当我尝试将它应用于整个文件夹时,我在地图中什么也没有得到。在调试完代码之后,我发现最后一个for循环没有被执行,这个循环是关于存储在map中的循环,但是我无法弄清楚这个循环的原因。我试图将 index ++; 移到for循环之上。代码输入了所提到的for循环,但最后我得到了相同的输出结果

解决方案

好吧,你正在进行一个讨厌的覆盖在这里 - 在每次迭代中,你都会丢失所有的值,并且你从0开始计数......

  //将它们存储到Maps 
for(Match m:Match.values()){
indexes.put(m,new ArrayList< Integer>());
}

另外,在最后一行离开内部循环之后,但是地图会被空的东西覆盖。



将上面的for块移动到创建索引图的部分之后。


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

解决方案

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.

这篇关于无法在映射java中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆