【发布时间】:2012-01-25 11:37:52
【问题描述】:
我正在编写一个从文本文件的行中获取数据的程序。问题是它不是最好的书面文本文件,在尝试为文件编写解析器时会有很多混乱
这里有两行这样的行,因为我都可以获得地址和纬度和经度变量,但在第二行我无法获得价格或尺寸。我不断收到的错误是-41(严重)的字符串越界异常
|12091805|,|0|,|DETAILS|,||,||,|Latitude:54.593406, Longitude:-5.934344 <b >Unit 8 Great Northern Mall Great Victoria Street Belfast Down<//b><p><p><p>Price : 150,000<p>Size: 2,411 Sq Feet ()<p>Rent : 50,500 Per Annum<p><p>Text<p><p>|,||,||
|15961081|,|0|,|DETAILS|,||,||,|<p>Latitude:54.593406, Longitude:-5.934344 <b>3-5 Market Street Lurgan BT66</b> </p> <p> </p> <p> </p> <p> Price : £250,000 </p> <p> Size: 0.173 acres (0.07ha) </p> <p> </p> <p> Text </p> <p> </p> <p> Text </p> <p> </p> <p> Text </p> <p> </p> <p> </p>|,||,||
它更长,但我现在只是为了说文字而更改了段落。
不,我不能重写文本文件。任何指针将不胜感激
if (s.contains("Price"))
{
int pstart = 0;
int pend = 0;
if (s.contains("<p>Size"))
{
//if has pound symbol
if (s.contains("£"))
{
String[] str = s.split("£");
StringBuilder bs = new StringBuilder();
for (String st : str)
{
bs.append(st);
}
pstart = bs.indexOf("Price") + 8;
pend = bs.indexOf("</p>") - 1;
}
else
{
pstart = s.indexOf("Price") + 8;
pend = s.indexOf("<p>Size");
}
String sp = s.substring(pstart, pend);
String[] spl = sp.split(",");
StringBuilder build = new StringBuilder();
for (String st : spl)
{
build.append(st);
f = build.toString();
}
in = Integer.parseInt(f);
p.setPrice(in);
}
else
{
if (s.contains("£"))
{
String[] str = s.split("£");
StringBuilder bs = new StringBuilder();
for (String st : str)
{
bs.append(st);
}
pstart = bs.indexOf("Price : ");
pend = bs.indexOf("</p>") - 1;
}
else
{
pstart = s.indexOf("Price") + 8;
pend = s.indexOf("<p>Size");
}
String sp = s.substring(pstart, pend);
String[] spl = sp.split(",");
StringBuilder build = new StringBuilder();
for (String st : spl)
{
build.append(st);
f = build.toString();
}
in = Integer.parseInt(f);
p.setPrice(in);
}
}
// if has size property
if (s.contains("Size"))
{
//if in acres
if (s.contains("acres"))
{
int sstart = s.indexOf("Size:") + 6;
int send = s.indexOf("acres") - 1;
String sp = s.substring(sstart, send);
double d = Double.parseDouble(sp);
p.setSized(d);
}
if (s.contains("()"))
{
int sstart = s.indexOf("Size:") + 6;
int send = s.indexOf("Sq") - 2;
String sp = s.substring(sstart, send);
if (sp.contains("-") && sp.contains(","))
{
String[] spl = sp.split("-|,");
StringBuilder str = new StringBuilder();
str.append(spl[0] + spl[1]);
StringBuilder str2 = new StringBuilder(0);
str2.append(spl[2] + spl[3]);
String s1 = str.toString();
int i = Integer.parseInt(s1);
p.setSize(i);
String s2 = str2.toString();
i = Integer.parseInt(s2);
p.setSize2(i);
}
if (sp.contains("-"))
{
String[] spl = sp.split("-");
int one = Integer.parseInt(spl[0]);
p.setSize(one);
int two = Integer.parseInt(spl[1]);
p.setSize2(two);
}
else if (!(sp.contains("-")))
{
if (sp.contains(","))
{
String[] spl = sp.split(",");
StringBuilder build = new StringBuilder();
for (String st : spl)
{
build.append(st);
f = build.toString();
}
in = Integer.parseInt(f);
p.setSize(in);
}
else
{
p.setSize(Integer.parseInt(sp));
}
}
}
}
v.add(p);
p = new Property();
【问题讨论】:
-
如果您向我们展示文件的外观示例会有所帮助
-
是文本文件还是html文件?
-
抱歉,添加到行数据中
-
文件有多大?如果它不是很大,只需将标签
... 替换为普通拆分器,无论是昏迷还是管道或其他任何东西。使用 sed 这样做。你会节省很多时间而不是浪费时间。
-
...... 用 sed 或 replace() 用逗号替换标签,然后使用你的 .split() 就可以了。
标签: java file parsing variables text