编辑*:就目前而言,我有一个程序以某种方式陷入了无限循环,但我不确定是什么导致了它。我的程序从文本文件中读取数据,本质上是“锯断”每一行的第一部分数据,直到第一个出现',‘字符,然后读取每个后续字符串(在一般情况下以’‘结尾),直到下一个',’(特殊情况,表示后面的数据也是垃圾数据)。从那里,它跳到下一行并重复。几周前,我的程序还运行得很好,但我对它进行了修补,现在它正式崩溃了。下面的编辑是我遇到的一些其他错误(我可能仍然需要解决),但目前,我正在被这个循环杀死。诚然,我很擅长使用嵌套的for循环,使用时髦的迭代器和更新,但这一点我就是不能破解。以下是代码,并有一些解释。这个完整的代码片段被抛到某个任意类的构造函数中,它接受一个字符串"synsets“,并且在我的main中创建了一个实例。剩下的我已经评论得尽可能好了:
In in = new In(synsets); // Custom input stream class, courtesy
// of Princeton U
Out fout = new Out("log.txt"); // ostream analogue
int linecount=0; // Marker used to keep track of line # in input file
int nouncount=0; // Marker for keeping track of the number of
// "important" data items
// Data comes in the format:
// "junk,important important important,junk
// junk,important,junk
// junk,important important,junk" etc.
{
int i=0;
for ( String str=in.readLine();str!=null;str=in.readLine() )
{
i=0; // reset iterator for a new line
for ( char next=str.charAt(i); next!=',';next=str.charAt(i) )
i++; // This FOR loop cuts out the junk at the start of
// a line
i++; // increment to after first comma
for ( char next=str.charAt(i);next!=',';next=str.charAt(i))
{
for (; next!=',' && next!=' ' ;next=str.charAt(i))
{
i++;
fout.print(next);
// count the "nouns" (important data) on a line
}
nouncount++;
// count the last noun on line, and subsequently fall
// through loops to skip the rest of the junk at the
// current line
fout.print('\n');
}
}
fout.print(nouncount);
in.close();
我的输出文件是好的,直到输入的第18行,在这一点上,它只是开始打印新的行字符(就像它被附加它们的循环所捕获,但不能进入下一个循环)。下面是有问题的输入行。它读取24K黄金很好,但由于某种原因无法写入pure_gold。有什么想法吗?谢谢大家!
“17,24克拉黄金pure_gold,100 %黄金
18, 24 /7,正常运行时间是一周7天,每天24小时“
编辑*:我正在编写一个程序,以便从文本文件中读取一些数据,并且得到一个新的(对我来说)错误消息,我已经用完了堆空间。我试着修补这个bug,但只成功地摆脱了错误消息。我正在读取的文本文件大约有90K行,但我的程序在第18行抛出了消息。我的修补工作确实揭示了一些事情,特别是,我的程序至少能够对整个文件进行一些处理,并且这个问题可能涉及到一些数据损坏。我推断这是因为我使用了一个数组索引"j“,它触发了一个IndexOutOfBoundsException。问题是,j在每次递增时仅按1缩放,并且不知何故,它超出了超过数组界限150,000的界限。因此,我假设"j“以某种方式被数据填充(可能是因为堆栈正在重写到堆中?)它与"j“或所讨论的数组无关。下面是我最初的帖子,尽管我承认我很累,所以它可能没有太大的意义。谢谢大家!:)
编辑**:我检查了nouncount,它是399850,所以我没有遇到数据损坏,正如我所怀疑的那样。我的一个循环就是无限地执行ad。我会试着找出是哪一个,如果我能纠正这个问题,我会发回来的。我至少会回来整理一下这篇文章,以供将来有需要的人参考。
我正在写一个读入文本文件的程序,它必须进行一些计数(文件中的数据以一种非常语法的方式进行划分),但是我的代码不能走得很远。虽然这个文件非常大(大约90K行),但我的代码在堆溢出之前只读完了17行。
在一些背景下," in“类只是一个专门的输入流,而真正杀死我的不是第一个FOR循环,它是我计算行数的地方(代码很好地执行这一部分,分别读取每一行,每一次)。相反,这是第二部分,但我不明白,因为我没有使用任何额外的堆空间(我想?)在那部分。我以前将这些块放在一起,逐行执行主要功能,但同样,程序会停在第17行。我尝试在Dr Java中分配高达1 GB的堆空间,但没有效果。在下半部分中有没有我可以留出额外堆空间的部分?
In in = new In(synsets);
StringBuilder nounData = new StringBuilder();
int linecount=0;
int nouncount=0;
{
String str;
int i=0;
char next='\0';
for ( str=in.readLine();str!=null;str=in.readLine() )
{
linecount++;
}
in.close();
in = new In(synsets);
for ( str=in.readLine();str!=null;str=in.readLine() )
{
i=0;
// The first portion of each line is "trash" until the first comma
for ( next=str.charAt(i) ; next!=',' ; next=str.charAt(++i) ){}
i++;
// This actually reads/processes the data until the next comma, then
// jumps to the next line. "What" i need done is really secondary, I
// just need to figure out what is eating so much space so I can
// trim it
for ( next=str.charAt(i);next!=',';next=str.charAt(i))
for (;next!=','&&next!=' ';next=str.charAt(++i))
nouncount++;
}
}
下面是更新后的代码,其中我尝试重新创建来自Java博士的Heap消息。虽然我不能这样做,但我得到了一个有趣的错误消息(当然是在第18行),关于数组越界。但是仍然被难住了,因为我看不出问题中的变量在命中时怎么会超过17。
代码如下:
In in = new In(synsets);
StringBuilder nounData = new StringBuilder();
int linecount=0;
int nouncount=0;
{
int i=0;
for ( String str=in.readLine();str!=null;str=in.readLine() )
{
i=0;
for ( char next=str.charAt(i);next!=',';next=str.charAt(i))
for (;next!=','&&next!=' ';next=str.charAt(++i))
nouncount++;
}
in.close();
in=new In(synsets);
String[] nouns = new String[nouncount];
int j=0;
for ( String str=in.readLine();str!=null;str=in.readLine() )
{
linecount++;
i=0;
for ( char next=str.charAt(i) ; next!=',' ; next=str.charAt(++i) ){}
i++;
for ( char next=str.charAt(i);next!=',';next=str.charAt(i))
{
for (;next!=','&&next!=' ';next=str.charAt(++i))
nounData.append(next);
nouns[j++]=nounData.toString();
nounData.delete(0,nounData.capacity()-1);
}
System.out.println("Current line count is: " + linecount);
}
}
in.close();
System.out.println("line count = "+linecount);
System.out.println("noun count = "+nouncount);
String[] nouns = new String[nouncount];
下面是错误消息:
Current line count is: 1
Current line count is: 2
Current line count is: 3
Current line count is: 4
Current line count is: 5
Current line count is: 6
Current line count is: 7
Current line count is: 8
Current line count is: 9
Current line count is: 10
Current line count is: 11
Current line count is: 12
Current line count is: 13
Current line count is: 14
Current line count is: 15
Current line count is: 16
Current line count is: 17
java.lang.ArrayIndexOutOfBoundsException: 399850
at WordNet.<init>(WordNet.java:39)
at WordNet.main(WordNet.java:212)
根据记录,我的代码中的"39“行是:
nouns[j++]=nounData.toString();
文件中的字符数一直到第18行是917,在第19行是966,所以我不认为我把该行放错地方了。
编辑:此外,我做了一个测试,文件中只有大约147K的“名词”,所以我猜测"j“不知何故被破坏了,因为它必须从0到147K之间的某个东西”跳“到399K+。不幸的是,我已经过了睡觉时间了,所以今晚我不能继续更新了,但请随意发布任何想法,明天早上我会通过电子邮件检查:)谢谢大家!
发布于 2015-04-16 04:47:52
更改所有for
测试条件
next!=','
至
next != ',' && i < str.length()
除了你的最后一个,
for (;next!=','&&next!=' ';next=str.charAt(++i))
这应该是
for (;next!=','&&next!=' '&&i+1<str.length();next=str.charAt(++i))
接下来,我将尝试使用String.split(String regex)
String[] words = str.split(",\\s+);
你可以用words.length
来计算单词数。要获取行计数,请在调用readLine()
时递增计数器,如
in = new In(synsets);
// for ( str=in.readLine();str!=null;str=in.readLine() )
while ((str = in.readLine()) != null) {
linecount++;
String[] words = str.split(",\\s+);
nouncount += words.length;
}
https://stackoverflow.com/questions/29665401
复制