我有将字符串分成3个字符串然后打印它们的代码。我希望每个人都能被“Enter”等同分开。下面的代码:用“Enter”分隔字符串
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
String[] tempArr = accval.split("\\s+");
String x = tempArr[0] + "_"; //I want the enter to be where the underlines are:
String y = tempArr[1] + "_";
String z = tempArr[2] + "_";
for (String a : tempArr) {
writer.println("<h3 style=padding-left:20px;>" + x + y
+ z + "</h3><br>");
}
writer.flush();
writer.close();
} catch (IOException e) {
// put notification here later!!!
e.printStackTrace();
}
此输出:
x=-0.125_y=0.9375_z=0.375
x=-0.125_y=0.9375_z=0.375
用下划线分隔字符串。
不过,我希望它看起来像这样:
x=-0.125
y=0.9375
z=0.375
x=-0.125
y=0.9375
z=0.375
感谢您的帮助。
编辑:
我实现@Julius的答案在下面的代码打印我怎么想它:
代码:
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
String[] tempArr = accval.split("\\s+");
String x = tempArr[0];
String y = tempArr[1];
String z = tempArr[2];
for (String a : tempArr) {
writer.println("<h3 style=padding-left:20px;>" + x + "<br>" + y + <br>
+ z + "</h3><br>");
}
writer.flush();
writer.close();
} catch (IOException e) {
// put notification here later!!!
e.printStackTrace();
}
它打印:
x=0.25
y=125
z=1.23
x=0.125
y=725
z=0.935
谢谢你:)如果我可以同时接受贾森C和朱利叶斯的答案,我会的,他们都很好。再次感谢 – Gurfuffle
Re:换行符。我将它保存到一个html文件中,并且我刚刚启动了java,并且正在为一个项目执行此操作,所以我的代码被扔在一起。感谢指出虽然:) – Gurfuffle