英文:
Why cant I acess my getters and setters from the main?
问题
以下是您要翻译的内容:
我正在尝试解决一个问题,该问题从文件中收集信息并将其存储在ArrayList中。我似乎无法弄清楚为什么无法访问我创建的类中的方法。在文件的每一行中,您都有两个团队的名称以及每场比赛的得分。我需要询问用户他想要我查找哪个团队,如果我能找到该名称,就呈现团队所玩的总比赛次数。我还需要比较比分并打印赢得的总比赛次数和输掉的总比赛次数。我的想法是创建一个方法来将字符串拆分为多个部分,并将这些信息作为属性添加到ArrayList中的新对象中。
以下是类Game:
public class Game {
String homeTeam;
String visitorTeam;
int homePoints;
int visitorPoints;
public Game(String home, String visitor, int hPoints, int vPoints){
this.homeTeam = home;
this.visitorTeam = visitor;
this.homePoints = hPoints;
this.visitorPoints = vPoints;
}
public String getHomeTeam() {
return homeTeam;
}
public String getVisitorTeam() {
return visitorTeam;
}
public int getHomePoints() {
return homePoints;
}
public int getVisitorPoints() {
return visitorPoints;
}
@Override
public String toString(){
return homeTeam + visitorTeam + homePoints + visitorPoints;
}
}
以下是目前为止的主要内容:
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
public class SportStatistics {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("File:");
String file = scan.nextLine();
System.out.println("Team:");
String team = scan.nextLine();
ArrayList<Game> gameInfo = readRecordsFromFile(file);
System.out.println(gameInfo);
gameInfo.getHomeTeam();
}
public static ArrayList<Game> readRecordsFromFile(String file) {
ArrayList<Game> information = new ArrayList<>();
try (Scanner read = new Scanner(Paths.get(file))) {
while (read.hasNextLine()) {
String info = read.nextLine();
if (info.isEmpty()) {
continue;
}
String[] parts = info.split(",");
String homeTeam = parts[0];
String visitorTeam = parts[1];
int homePoints = Integer.valueOf(parts[2]);
int visitorPoints = Integer.valueOf(parts[3]);
information.add(new Game(homeTeam, visitorTeam, homePoints, visitorPoints));
}
} catch (Exception e) {
}
return information;
}
}
英文:
I am trying to solve a problem that collects information from a file and stores it in an ArrayList. I cant seem to figure out why I cant access the methods from the class I created. In each line of the file you have the name of two teams and its scores in each game. I need to ask the user which team he wants me to look for and if I can find the name, present the totalGames the team played. I would also need to compare the scores and print total games won and total games lost. My idea was creating a method to split the string in multiple parts and add those info as attributes to a new object in an ArrayList.
Here is the class Games:
public class Game {
String homeTeam;
String visitorTeam;
int homePoints;
int visitorPoints;
public Game(String home, String visitor,int hPoints,int vPoints){
this.homeTeam = home;
this.visitorTeam = visitor;
this.homePoints = hPoints;
this.visitorPoints = vPoints;
}
public String getHomeTeam() {
return homeTeam;
}
public String getVisitorTeam() {
return visitorTeam;
}
public int getHomePoints() {
return homePoints;
}
public int getVisitorPoints() {
return visitorPoints;
}
@Override
public String toString(){
return homeTeam + visitorTeam + homePoints + visitorPoints;
}
}
and here is how my main is looking like so far:
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
public class SportStatistics {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("File:");
String file = scan.nextLine();
System.out.println("Team:");
String team = scan.nextLine();
ArrayList<Game> gameInfo = readRecordsFromFile(file);
System.out.println(gameInfo);
gameInfo.getHomeTeam();
}
public static ArrayList<Game> readRecordsFromFile(String file) {
ArrayList<Game> information = new ArrayList<>();
try ( Scanner read = new Scanner(Paths.get(file))) {
while (read.hasNextLine()) {
String info = read.nextLine();
if (info.isEmpty()) {
continue;
}
String[] parts = info.split(",");
String homeTeam = parts[0];
String visitorTeam = parts[1];
int homePoints = Integer.valueOf(parts[2]);
int visitorPoints = Integer.valueOf(parts[3]);
information.add(new Game(homeTeam, visitorTeam, homePoints, visitorPoints));
}
} catch (Exception e) {
}
return information;
}
}
答案1
得分: 0
gameInfo在这里是ArrayList类型。您将需要首先迭代该列表,然后才能调用getter和setter方法。
gameInfo.forEach(ob -> System.out.println(ob.getHomeTeam()));
英文:
gameInfo is of type ArrayList here. You will have to iterate the list first the you will be able to call getters and setters.
gameInfo.forEach(ob->System.out.println(ob.getHomeTeam()));
答案2
得分: 0
for(Game g : gameInfo){
System.out.println(g.getHomeTeam());
}
This should do it a simple for each loop to grab each game out of the list (g) and prints the home team for each game.
You can also do
for(int i = 0; i < gameInfo.size(); i++){
System.out.println(g.getHomeTeam());
}
英文:
for(Game g : gameInfo){
System.out.println(g.getHomeTeam());
}
This should do it a simple for each loop to grab each game out of the list (g) and prints the home team for each game.
You can also do
for(int i = 0; i < gameInfo.size(); i++){
System.out.println(g.getHomeTeam());
}
答案3
得分: 0
gameInfo是一个Game类的数组列表。您需要遍历该列表以获取对象。只有使用这些对象,才能访问其getter和setter方法。
您可以使用for循环或for-each循环来实现此目的。
英文:
gameInfo is a arrayList of class Game. You will have to iterate through the list to get the object. The getters and setters can only be accessed with these objects.
You can use a for loop or a for-each loop for this purpose.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论