用 Java 解析文本文件
输入文件的示例:
ARTIST="unknown"
TITLE="Rockabye Baby"
LYRICS="Rockabye baby in the treetops
When the wind blows your cradle will rock
When the bow breaks your cradle will fall
Down will come baby cradle and all
"
艺术家、标题和名称歌词字段必须提取到各自的字符串,且大写和格式保持不变。 Artist 字段的代码
while (readIn.hasNext()) {
readToken= readIn.next();
if (readToken.contains("ARTIST")) {
artist= readIn.next();
}
if (readToken.contains("TITLE")){
title= readIn.next();
System.out.print(artist+" "+title);
}
最终打印出以下内容:
"unknown"
TITLE null"unknown"
TITLE null"unknown"
TITLE
从代码中,我看不出为什么输出会以这种方式打印。每次循环时,readToken 字符串都会刷新,然后应通过 contains() 方法进行比较。显然我在这里遗漏了一些东西。
那么,我是否已经接近正确的轨道,还是身处完全不同的城市?
Example from input file:
ARTIST="unknown"
TITLE="Rockabye Baby"
LYRICS="Rockabye baby in the treetops
When the wind blows your cradle will rock
When the bow breaks your cradle will fall
Down will come baby cradle and all
"
The Artist, Title & Lyrics fields have to be extracted to their respective Strings with captalization and format unchanged. This code for the Artist field
while (readIn.hasNext()) {
readToken= readIn.next();
if (readToken.contains("ARTIST")) {
artist= readIn.next();
}
if (readToken.contains("TITLE")){
title= readIn.next();
System.out.print(artist+" "+title);
}
ends up printing this out:
"unknown"
TITLE null"unknown"
TITLE null"unknown"
TITLE
From the code, I can't see why the output is printing this way. For every time through the loop, the readToken string gets refreshed, and should then be compared by the contains() method. Obviously I'm missing something here.
So, am I close to the right track or am I in a completely different city?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的程序代码来看
,如果假设正确声明和实例化(变量 Artist、readToken 和 title)首先是
检查 while 条件中是否存在现有下一行,如果为真,则 String(假设)readToken 将保存为下一行。如果 readToken 包含“ARTIST”,则下一行将保存为艺术家字符串。对于包含“TITLE”也是如此。当 while 循环重复时,您可能已经点击了 LYRICS,完全跳过了 TITLE,导致它为 NULL。
你想要的也许是保存
Artist = readToken;
或标题= readToken;反而。
如果您不想打印“ARTIST =“ARTISTNAMEHERER”TITLE =“TITLENAMEHERE””而不是“ARTISTNAME,TITLENAME”,也不要忘记对艺术家和标题进行子字符串化
From your code of
The program, if assuming is declared and instantiated correctly (the variables artist, readToken and title) is firstly
checking if there exists an existing next line in the while condition, if that is true, the String (im assuming) readToken is saved as the next line. If readToken contains "ARTIST", then the NEXT line down is saved as the artist string. Likewise for containing "TITLE". By the time the while loop repetends, you might have already hit LYRICS, skipping the TITLE altogether, causing it to be NULL.
What you want is maybe saving
artist = readToken;
or title = readToken; instead.
Also don't forget to substring artist and title if you don't want a print of "ARTIST="ARTISTNAMEHERER" TITLE="TITLENAMEHERE"" and rather "ARTISTNAME, TITLENAME"
根据 Alex Hart 的回答,我认为您可能会考虑使用 Java 的 Pattern 和 Matcher 类,并使用组来获取匹配的参数,例如(未经测试):
然后当您阅读一行时:
Further to Alex Hart's answer, I think you might consider using Java's Pattern and Matcher classes and use groups to obtain the matched arguments e.g. (non tested):
Then when you're reading a line:
看起来您在这里所做的似乎是逐行阅读,但是当您找到所需内容时,您正在将变量设置为下一行。这可能会导致问题和越界问题,并且很可能是您的不幸事故的征兆
It looks as if what you are doing here is reading in line by line, but when you find what you are looking for, you are setting your variable to the next line. This may be causing issues and out of bounds problems, and could very well be a symptom of your mishap