Java string.length 未按预期工作
Java string.length not working as expected
我有一个包含以下格式数据的文件:
12345foo \n
24561bar \n
12783apples \n ect.
为了处理这些数据,我使用 bufferedreader 将其读入程序并使用以下代码将其放入二维数组中:
line = bRead.readLine();
2DArray[x][y] = line.substring(0,5);
2DArray[x][z] = line.substring(5,line.length());
我不确定为什么 line.length 在这种情况下显然给我 (line.length() - 5)
。
你应该这样做:
line.substring(5);
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)
public static void main(String[] args) {
BufferedReader bR = null;
try {
bR = new BufferedReader(new FileReader("teste.txt"));
Map<String, String> txt = new HashMap<String, String>();
String line;
while ((line = bR.readLine()) != null) {
txt.put(line.substring(0, 5), line.substring(5));
}
txt.forEach((k, v) -> System.out.println(k + " - " + v));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bR != null) {
try {
bR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Output:
24561 - bar
12345 - foo
12783 - apples
如果我必须批准和审查您的代码,我的第一个问题是 "Why are you using arrays?" 将逻辑上属于一起的数据拆分为二维数组或地图,我认为这是糟糕的代码。你显然有一个行项目,有一个 id 和一个名称,所以你应该有这样一个对象的域模型。
这是我会做的。
package com.example
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String data = "12345foo\n" +
"24561bar\n" +
"12783apples\n";
BufferedReader br = new BufferedReader(new StringReader(data));
String line = null;
List<LineItem> items = new ArrayList<>();
while (null != (line = br.readLine())) {
int id = Integer.parseInt(line.substring(0, 5));
String name = line.substring(5);
items.add(new LineItem(id, name));
}
items.forEach(lineItem -> System.out.println(lineItem));
}
public static class LineItem {
private final int id;
private final String name;
public LineItem(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "LineItem{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
在 java 中,line.substring() 使用第一个参数确定距起点的偏移量,并使用第二个参数确定...距起点的偏移量。例如,
String foo = "barAndSomeOtherStuff";
System.out.print(foo.substring (5, 12));
会输出 "dSomeOt",而不是 "dSomeOtherSt."
对于此类问题的一个好主意是在访问 Whosebug 之类的地方之前先查看文档!
我有一个包含以下格式数据的文件:
12345foo \n
24561bar \n
12783apples \n ect.
为了处理这些数据,我使用 bufferedreader 将其读入程序并使用以下代码将其放入二维数组中:
line = bRead.readLine();
2DArray[x][y] = line.substring(0,5);
2DArray[x][z] = line.substring(5,line.length());
我不确定为什么 line.length 在这种情况下显然给我 (line.length() - 5)
。
你应该这样做:
line.substring(5);
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)
public static void main(String[] args) {
BufferedReader bR = null;
try {
bR = new BufferedReader(new FileReader("teste.txt"));
Map<String, String> txt = new HashMap<String, String>();
String line;
while ((line = bR.readLine()) != null) {
txt.put(line.substring(0, 5), line.substring(5));
}
txt.forEach((k, v) -> System.out.println(k + " - " + v));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bR != null) {
try {
bR.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Output:
24561 - bar
12345 - foo
12783 - apples
如果我必须批准和审查您的代码,我的第一个问题是 "Why are you using arrays?" 将逻辑上属于一起的数据拆分为二维数组或地图,我认为这是糟糕的代码。你显然有一个行项目,有一个 id 和一个名称,所以你应该有这样一个对象的域模型。 这是我会做的。
package com.example
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String data = "12345foo\n" +
"24561bar\n" +
"12783apples\n";
BufferedReader br = new BufferedReader(new StringReader(data));
String line = null;
List<LineItem> items = new ArrayList<>();
while (null != (line = br.readLine())) {
int id = Integer.parseInt(line.substring(0, 5));
String name = line.substring(5);
items.add(new LineItem(id, name));
}
items.forEach(lineItem -> System.out.println(lineItem));
}
public static class LineItem {
private final int id;
private final String name;
public LineItem(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "LineItem{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
在 java 中,line.substring() 使用第一个参数确定距起点的偏移量,并使用第二个参数确定...距起点的偏移量。例如,
String foo = "barAndSomeOtherStuff";
System.out.print(foo.substring (5, 12));
会输出 "dSomeOt",而不是 "dSomeOtherSt."
对于此类问题的一个好主意是在访问 Whosebug 之类的地方之前先查看文档!