Java 从文本文件中获取值,然后将其放入没有重复项的列表中?
我有一个程序,它可以读取一个文本文件,该文件的数据格式为Java 从文本文件中获取值,然后将其放入没有重复项的列表中?,java,arrays,list,dictionary,Java,Arrays,List,Dictionary,我有一个程序,它可以读取一个文本文件,该文件的数据格式为 `FootballTeamName1 : FootballTeamName2 : FootballTeam1Score : FootballTeam2Score 当前,它读取文件并在每个冒号处拆分它。我想知道的是,如何使它在每次遇到一个名称时,它都会将该名称添加到列表中(如果该名称不存在),或者如果该名称不存在,则它不会创建重复的值,而是将该值添加到已存在的值中球队名称 我目前有一个程序,可以在搜索团队时获取其值,但我想做的是使其成为可
`FootballTeamName1 : FootballTeamName2 : FootballTeam1Score : FootballTeam2Score
当前,它读取文件并在每个冒号处拆分它。我想知道的是,如何使它在每次遇到一个名称时,它都会将该名称添加到列表中(如果该名称不存在),或者如果该名称不存在,则它不会创建重复的值,而是将该值添加到已存在的值中球队名称
我目前有一个程序,可以在搜索团队时获取其值,但我想做的是使其成为可能,当用户未指定团队时,它将使其成为可能,以便返回所有团队的结果。下面是我目前所做的,它询问文件的位置,并要求用户指定一个有效的文件,但我不确定如何做我想要的
import java.awt.Desktop;
import java.io.*;
import java.util.*;
public class reader {
//used to count the number of invalid and valid matches
public static boolean verifyFormat(String[] words) {
boolean valid = true;
if (words.length != 4) {
valid = false;
} else if (words[0].isEmpty() || words[0].matches("\\s+")) {
valid = false;
} else if ( words[1].isEmpty() || words[1].matches("\\s+")) {
valid = false;
}
return valid && isInteger(words[2]) && isInteger(words[3]);}
//checks to see that the number of items in the file are equal to the four needed and the last 2 are integers
//also checks to make sure that there are no results that are just whitespace
public static boolean isInteger( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception e ) {
return false;
}
}
//checks to make sure that the data is an integer
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
while(true){ //Runs until it is specified to break
Scanner scanner = new Scanner(System.in);
System.out.println("Enter filename");
String UserFile = sc.nextLine();
File file = new File(UserFile);
if(!file.exists()) {
continue;
}
if(UserFile != null && !UserFile.isEmpty()){
System.out.println("Do you want to generate plain (T)ext or (H)TML");
String input = scanner.nextLine();
if ( input.equalsIgnoreCase("H") ) {
processFile(UserFile) ; }
else if ( input.equalsIgnoreCase("T")){
processFile(UserFile);
}
else{
System.out.println("Do you want to generate plain (T)ext or (H)TML");
}
}
}
}
//checks how the user wants the file to be displayed
private static void processFile(String UserFile) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
int totgoals = 0;
int gamesplayed = 0;
int gs = 0;
int gc = 0;
int w = 0;
int d = 0;
int l = 0;
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.
Scanner s = new Scanner(new BufferedReader(
new FileReader(UserFile))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name of the team you want the results for");
String input = scanner.nextLine();
while (s.hasNext()) {
String line = s.nextLine();
String[] words = line.split("\\s*:\\s*");
//splits the file at colons
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
totgoals = totgoals + hscore;
ascore = Integer.parseInt(words[3]); //read the away team score
totgoals = totgoals + ascore;
if ( input.equalsIgnoreCase(hteam)){
gamesplayed = gamesplayed + 1;
gs = gs + hscore;
gc = gc + ascore;
if (hscore > ascore)
w = w + 1;
else if (ascore > hscore)
l = l + 1;
else
d = d + 1;
}
else if (input.equalsIgnoreCase(ateam)){
gamesplayed = gamesplayed + 1;
gs = gs + ascore;
gc = gc + hscore;
if (hscore < ascore)
w = w + 1;
else if (ascore < hscore)
l = l + 1;
else
d = d + 1;
}
}
}
System.out.println(input + newLine + "--------------------------" + newLine + "Games played: " + gamesplayed + newLine + "Games Won: " + w + newLine + "Games Drawn: " + d + newLine + "Games Lost: " + l + newLine + "Goals For: " + gs + newLine + "Goals Against: " + gc);
}
}
如果希望集合中没有重复的元素,请使用集合。集合是不包含重复元素文档的集合。您可以将条目添加到集合中,或者如果希望对其进行排序,也可以将其添加到SortedSet中。由于Set只保存唯一的值,所以每个值始终只有一个副本 作为参考,以下代码使用5个值,但集合将只有3个唯一值:
String str = "A : B : C : B : A";
String[] vals = str.split(":");
SortedSet<String> myVals = new TreeSet<String>();
for(String val : vals)
{
myVals.add(val.trim());
}
System.out.println("Set size: " + myVals.size());
听起来你想从一个团队到另一个得分的地图,如果团队已经存在,那么从地图中获取值,并将分数添加到地图中。阅读相关内容后,我知道如何将其添加到地图中,但我不知道如何只添加正确的,我该如何做才能让它添加第一个团队名称,然后添加分数,而不是将所有值添加到地图中?不确定你的意思,如果你想要一个结果列表,只需将团队映射到一个分数列表即可