假设我有这样的CSV文件:
Football Contest blabla bla,,,,,,,
Team number1,Team number2,Points team1,Points team2,Red cards
Sweden,France,1,2,"
Sweden,Brazil,3,5,2
Sweden,Germany,2,2,3
Sweden,Spain,3,5,"
在这个文件中,我只想打印出红牌的比赛。所以在这个例子中我想打印:
瑞典 - 巴西= 2瑞典 - 德国= 3
这是我目前的代码,我坚持如何继续前进。
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String lines = br.readLine();
String result[] = lines.split(",");
do{
System.out.println();
}while((lines = br.readLine()) != null);
//String result[] = lines.split(",");
//System.out.println(result[1]);
br.close();
}catch (FileNotFoundException e){
System.out.println("File not found : "+ file.toString());
}catch (IOException e ){
System.out.println("Unable to read file: "+ file.toString());
}
编辑我得到了帮助:
while (line != null) {
String result[] = line.split(",");
if (result.length == 5) { //red cards present?
System.out.println(result[0] + " - " + result[1] + " " + result[4]);
}
line = br.readLine(); //read next
}
但我现在遇到的问题是它仍然打印所有因为"在csv文件中。为什么我不能这样做?
if (result[4] == "1,2,3,4,5" ){
System.out.println(result[0] + " - " + result[1] + " " + result[4]);
}
答案 0 :(得分:0)
你的try-catch块应如下所示:
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
String result[] = line.split(",");
if (result.length == 5 && result[4].matches("[0-9]")) { //red cards present?
System.out.println(result[0] + " - " + result[1] + " " + result[4]);
}
line = br.readLine(); //read next
}
//close readers
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("File not found : " + file.toString());
} catch (IOException e) {
System.out.println("Unable to read file: " + file.toString());
}
如果文件中有尾随空格,则必须先修剪字符串:result[4].trim().matches("[0-9]")
或使用其他正则表达式:result[4].matches("\\d\\s")
答案 1 :(得分:0)
如果你是红牌的索引,它看起来像整数,那么看看该索引是否是整数,如果是,则打印0,1和4 index [0] =团队编号1 index [1] =第2队 index [4] =红牌
答案 2 :(得分:0)
为什么我不能做这样的事情?
if (result[4] == "1,2,3,4,5" ){
问题是==
对身份的测试:如果result[4]
中的引用与常量相同引用,它将进行比较在你的源代码中。那个表达总是假的。你需要检查是否平等,而不是身份:
if (Objects.equals(result[4], "1,2,3,4,5")) {
或
if (result[4] != null && result[4].equals("1,2,3,4,5")) {
或
if ("1,2,3,4,5".equals(result[4])) {
请注意,Objects.equals()
(最终)已添加到Java 8中的Java标准库中。如果没有它,则必须在对对象调用.equals()方法之前防止NullPointerException。传统上我更喜欢上一个版本,因为在字符串文字上调用方法意味着我可以放心,它永远不会为空,只会有效。
答案 3 :(得分:0)
您可以尝试这样 公共类HelloWorld {
public static void main(String []args){
String str1= "Sweden,Brazil,3,5,4";
String str2="Sweden,Germany,2,2,3";
String str3="Football Contest blabla bla,,,,,,,";
String result1[]=str1.split(",");
String result2[]=str2.split(",");
String result3[]=str3.split(",");
if(result1.length>=5){
System.out.print(result1[0]+"-"+result1[1]+"="+result1[4]);
System.out.println();
}
if(result2.length>=5){
System.out.print(result2[0]+"-"+result2[1]+"="+result2[4]);
System.out.println();
}
if(result3.length>=5){
System.out.print(result3[0]+"-"+result3[1]+"="+result3[4]);
System.out.println();
}
}
}
答案 4 :(得分:-2)
试试这个:
do {
if (result[4] instanceof Integer) {
System.out.print(result[0]+"="+result[1]+"="+result[4])
}
} while ((lines = br.readLine()) != null);