java读取文件并对内容进行排序直接输出结果
文件内容是idheadachemuscle-paintemperatureflue1yesyesnormalnoe2yesyeshighyese3yesyesveryhi...
文件内容是
id headache muscle-pain temperature flu
e1 yes yes normal no
e2 yes yes high yes
e3 yes yes very high yes
e4 no yes normal no
e5 no no high no
e6 no yes very high yes
e7 yes no high yes
e8 no yes normal no
e9 yes yes normal yes
e10 no no high yes
要求输出结果为
id headache muscle-pain temperature flu
e1 yes yes normal no
e2 yes yes high yes
e3 yes yes very high yes
e7 yes no high yes
e9 yes yes normal yes
e4 no yes normal no
e5 no no high no
e6 no yes very high yes
e8 no yes normal no
e10 no no high yes
谢谢大神!!
在线等啊~~~ 展开
id headache muscle-pain temperature flu
e1 yes yes normal no
e2 yes yes high yes
e3 yes yes very high yes
e4 no yes normal no
e5 no no high no
e6 no yes very high yes
e7 yes no high yes
e8 no yes normal no
e9 yes yes normal yes
e10 no no high yes
要求输出结果为
id headache muscle-pain temperature flu
e1 yes yes normal no
e2 yes yes high yes
e3 yes yes very high yes
e7 yes no high yes
e9 yes yes normal yes
e4 no yes normal no
e5 no no high no
e6 no yes very high yes
e8 no yes normal no
e10 no no high yes
谢谢大神!!
在线等啊~~~ 展开
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
File file = new File("data.txt");
List<String> list = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.ready()) {
list.add(br.readLine());
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = list.size() - 1; i > 1; i--) {
for (int j = 1; j < i; j++) {
String[] split1 = list.get(j + 1).split("\t");
String[] split2 = list.get(j).split("\t");
if (split1[1].compareTo(split2[1]) > 0) {
String temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
更多追问追答
追问
谢谢,可以输出,能否帮我把后面的内容与第一行对齐么?我弄了好久对不齐
追答
对齐不太容易,我试试吧
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
File file = new File("data.txt");
List<String> list = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.ready()) {
list.add(br.readLine());
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = list.size() - 1; i > 1; i--) {
for (int j = 1; j < i; j++) {
String[] split1 = list.get(j + 1).split("\t");
String[] split2 = list.get(j).split("\t");
if (split1[1].compareTo(split2[1]) > 0) {
String temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
for (int i = 0; i < list.size(); i++) {
String[] split = list.get(i).split("\t");
System.out.printf("%-8s%-16s%-16s%-16s%-8s\n",
split[0], split[1], split[2], split[3], split[4]);
}
}
}
2014-07-08
展开全部
行排序直接输出结果
10 分钟前StarのLight | 分类:JAVA相关 | 浏览9次
10 分钟前StarのLight | 分类:JAVA相关 | 浏览9次
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询