用 Java 解析文本文件

发布于 2024-09-19 01:49:50 字数 815 浏览 5 评论 0原文

输入文件的示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦言归人 2024-09-26 01:49:50

从您的程序代码来看

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);
}

,如果假设正确声明和实例化(变量 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

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);
}

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"

糖果控 2024-09-26 01:49:50

根据 Alex Hart 的回答,我认为您可能会考虑使用 Java 的 Pattern 和 Matcher 类,并使用组来获取匹配的参数,例如(未经测试):

private static final Pattern RECORDING_HEADER = 
  new Pattern("(ARTIST=\\"(.*)\\")?(TITLE=\\"(.*)\\")?(LYRICS=\\"(.*)\\")?");

然后当您阅读一行时:

String line = readIn.readLine(); // Presuming that readIn is a BufferedReader
Matcher m = RECORDING_HEADER.matcher(line);

if (m.matches()) {
  final int artistGroup = 2;
  String artist = m.group(artistGroup);

  final int titleGroup = 4;
  String title = m.group(titleGroup);

  final int lyricsGroup = 6;
  String lyrics = m.group(lyricsGroup);

  if (artist != null) {
    // You've got an artist...
  } else if (title != null) {
    // etc...
  }
}

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):

private static final Pattern RECORDING_HEADER = 
  new Pattern("(ARTIST=\\"(.*)\\")?(TITLE=\\"(.*)\\")?(LYRICS=\\"(.*)\\")?");

Then when you're reading a line:

String line = readIn.readLine(); // Presuming that readIn is a BufferedReader
Matcher m = RECORDING_HEADER.matcher(line);

if (m.matches()) {
  final int artistGroup = 2;
  String artist = m.group(artistGroup);

  final int titleGroup = 4;
  String title = m.group(titleGroup);

  final int lyricsGroup = 6;
  String lyrics = m.group(lyricsGroup);

  if (artist != null) {
    // You've got an artist...
  } else if (title != null) {
    // etc...
  }
}
じее 2024-09-26 01:49:50

看起来您在这里所做的似乎是逐行阅读,但是当您找到所需内容时,您正在将变量设置为下一行。这可能会导致问题和越界问题,并且很可能是您的不幸事故的征兆

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文