在映射java中插入值失败
我正在尝试将值插入java中的映射以循环它们并进行一些计算,因此我正在读取一个包含11k文件的文件夹,其中包含以下内容: ps:所有文件都具有相同的结构在映射java中插入值失败,java,hashmap,Java,Hashmap,我正在尝试将值插入java中的映射以循环它们并进行一些计算,因此我正在读取一个包含11k文件的文件夹,其中包含以下内容: ps:所有文件都具有相同的结构 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 1
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
并通过下面的代码检查它们
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);
}
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.StringTokenizer;
公共类z{
//枚举以将元音与字符串值匹配
公共枚举匹配{
_aa(“aa”)、_ii(“ii”)、_uu(“uu”);
公共字符串值;
私有匹配(字符串值){
这个值=值;
}
公共静态匹配fromString(字符串s){
for(匹配m:Match.values()){
if(s.子串(4,6).等于(m.值))
返回m;
}
返回null;
}
}
公共静态void main(字符串[]args)引发NumberFormatException,
IOException{
**//阅读文件夹**
File folderPhseg=新文件(
“/home/bassem/workspace/outputs/new”);
文件[]listOfPhseg=folderPhseg.listFiles();
映射索引=新的HashMap();
for(匹配m:Match.values()){
index.put(m,newarraylist());
}
字符串行=”;
for(文件:listOfPhseg){
if(file.isFile()){
FileReader inputFile=新的FileReader(file.getAbsolutePath());
BufferedReader bufferReader=新的BufferedReader(inputFile);
String[]column1=新字符串[100];
String[]column2=新字符串[100];
String[]column3=新字符串[100];
String[]column4=新字符串[100];
String[]column5=新字符串[100];
int指数=0;
//将其转换为五个数组
而((line=bufferReader.readLine())!=null){
字符串temp=“”;
整数计数=1;
第4列[索引]=“”;
//系统输出打印项次(行);
StringTokenizer st=新的StringTokenizer(行“”);
//字符串标记器从每个空间获取标记
而(st.hasMoreTokens()){
温度=圣奈克特肯();
如果(温度等于(“总”)){
打破
}
//解析输入
如果(计数=1){
第1列[索引]=温度;
}
如果(计数=2){
第2列[索引]=温度;
}
如果(计数=3){
第3列[索引]=温度;
}
如果(计数=4){
第4列[索引]=温度;
}
如果(计数=5){
第5列[索引]+=温度;
}
如果(计数小于5)
计数++;
}
}
//将它们存储到地图中
对于(int k=0;k
我使用键Match
将这些数据存储到地图中,然后当我尝试打印地图时,会得到一个空的
我在其中一个文件上尝试了这段代码,它对我很有效。
问题是,当我尝试将其应用于整个文件夹时,我在地图中什么也得不到。调试完代码后,我发现最后一个for循环没有执行,而这个循环与存储在map中有关,但我无法找出背后的原因。我试图移动
index++代码>位于for循环的上方。代码进入了前面提到的for循环,但在最后我得到了相同的输出好吧,你在这里做了一个糟糕的覆盖-在每次迭代中,你丢失了所有的值,然后从零开始计数
//storing them into Maps
for (Match m : Match.values()) {
indexes.put(m, new ArrayList<Integer>());
}
//将它们存储到地图中
for(匹配m:Match.values()){
index.put(m,newarraylist());
}
同样,在最后一行之后,你“打破”了内部循环,但是地图将被空的东西覆盖
将上面的“for”块移到创建“index”映射的部分之后。A)您的问题是什么B)使用调试器,您的任何问题都将归结为您要求我们为您调试代码。@redFIVE我在编写问题时误按了Enter
!!然后我编辑了itOk,那么我关于调试自己的代码的观点是如何降低有效性的呢?你在任何地方都没有问题,只是关于你不能做的陈述在文本区域按Enter键并不能保存问题。@Tom我为什么要对你或他撒谎?!他在提交我的问题后不到一分钟就写了一篇评论。顺便说一句,我在编辑完我的问题后阅读了他的评论。这个循环是为了用我在枚举中的3个键创建我的打印地图的结构。当我删除它时,我实际上得到了一个空输出,就像这样{}
,代码仍然不能进入循环,但是当我有了它时,我有了这个{uu-uu=[],\u-ii=[],\u-aa=[]
这正是我需要的。输出整数应该存在于它们的匹配中,但我得到了一个空整数我说你应该