gpt4 book ai didi

java - 处理文本文件中的字符串并继续执行 java 程序

转载 作者:行者123 更新时间:2023-12-01 17:11:53 27 4
gpt4 key购买 nike

文本文件:

9310,12,120,2,1
9333,12,120,2,1
PRINT
9533,5,45,0,0
8573,10,120,1,0
6343,6,18,170,0
PRINT
9311,12,120,2,1
3343,7,20,220,0

代码

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
i mport java.util.Comparator;
import java.util.List;


public class FantasyPlayers {

int player_ID;
int total_score;

public FantasyPlayers(int player, int total) {
player_ID = player;
total_score = total;
}

public String toString() {
return player_ID + "," + total_score;
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileInputStream fstream = new FileInputStream("Stats.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

String delims = "[,]";
List<FantasyPlayers> Stats = new ArrayList<FantasyPlayers>();
int a = 2;
int b = 1;
int c = 6;
int d = -1;
for (String line; (line = br.readLine()) != null;)
{
String[] parsedData = line.split(delims);
if (line != "PRINT")
{
int score = Integer.parseInt(parsedData[1])*a + Integer.parseInt(parsedData[2])*b + Integer.parseInt(parsedData[3])*c + Integer.parseInt(parsedData[4])*d ;
Stats.add(new FantasyPlayers(Integer.parseInt(parsedData[0]), score));
}
else
continue;
}
br.close();

System.out.println("Unordered");
for (FantasyPlayers str : Stats)
{
System.out.println(str);
}
Collections.sort(Stats, new Comparator<FantasyPlayers>()
{
public int compare(FantasyPlayers one, FantasyPlayers two)
{
if (one.total_score == two.total_score){
Integer playerOne = one.player_ID;
Integer playerTwo = two.player_ID;

return playerTwo.compareTo(playerOne);
}
Integer scoreOne = one.total_score;
Integer scoreTwo = two.total_score;

return scoreTwo.compareTo(scoreOne);
}
});
System.out.println("Ordered");
for (FantasyPlayers str : Stats)
{
System.out.println(str);
}

}

}

我在 int Score = 位置不断收到错误 ArrayIndexOutOfBoundsException:1。

我的问题是如何处理代码中的 PRINT 语句...我可以对数据执行我想要的操作,但是当我读取 print 语句时,我需要逻辑来执行某些操作。有什么建议吗?

最佳答案

你,我的 friend ,陷入了最常见的 java 错误......

试试这个

   if (!line.equalsIgnoreCase("PRINT"))

关于java - 处理文本文件中的字符串并继续执行 java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23430578/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com