java - 将数据输出到Java中另一个类中的String

标签 java arrays list

我目前无法从列表中提取某些值。我要打印theData到另一个名为 Card 的类中的 String我不知道该怎么做:

private List<FootballPlayer> getData() {
    List<FootballPlayer> theData = new ArrayList<FootballPlayer>();
    // create a Scanner and grab the data . . .
    Scanner scanner = null;
    String dataPath = dbLocation + File.separator + "text" + File.separator + "players.db";
    String imagePath = dbLocation + File.separator + "images";
    try {
        scanner = new Scanner(new File(dataPath));
    } catch (FileNotFoundException fnf) { 
        System.out.println(fnf.getMessage());
        System.exit(0);
    }

    // scan player.db file line-by-line
    scanner.useDelimiter("\n");
    while (scanner.hasNext()) {
        String line = scanner.next().trim();
       // trim used to trim for new line
        String[] bits = line.split(":");
        String t = bits[0];                    // title
        String imgFileName = bits[1];          // image file name
        int pa = Integer.parseInt(bits[2]);     // pace
        int sh = Integer.parseInt(bits[3]);     // shooting
        int dr = Integer.parseInt(bits[4]);    // dribbling
        int ph = Integer.parseInt(bits[5]);    // physical
        // create the image
        ImageIcon img = new ImageIcon(imagePath + File.separator +  imgFileName);
      // Create the business object
       FootballPlayer player = new FootballPlayer(t, img, pa, sh, dr, ph);
      // add it to the list ... simple as ...
      theData.add(player);
}
  scanner.close(); 
  return theData;

}

如果有帮助的话,我的players.db目前看起来像这样:

Pique:DFPique.png:66:60:54:76
Ramos:DFRamos.png:79:66:60:82
Maldini:LEGMaldini.png:86:67:56:80
Pele:LEGPele.png:95:94:89:73
DiMaria:MFDiMaria.png:90:87:79:71
Neymar:MFNeymar.png:90:92:80:58
Berahino:STBerahino.png:81:70:69:55
Ronaldo:STRonaldo.png:98:99:99:89

最佳答案

我认为你的问题可能出在另一个类中,如果你想输出数组列表中的内容,你必须迭代它,并且你的足球运动员需要一个字符串方法,你可以使用这个对象是你的类,它有你的您发布的 getdata 方法

for(FootballPlayer each: object.getData())
System.out.println(each.toString());

关于java - 将数据输出到Java中另一个类中的String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471429/

相关文章:

java - 如何使用现有的 Java 类运行服务器来接受来自另一个应用程序的 API 调用?

c# - Silverlight C# 是否有将集合转换为文件以及相反的标准方法?

perl - 如何使用现有数组作为 Perl 散列中的值?

java - 如何在 Java 中创建移动平均线

python - 将字符串差异与字符串列表进行比较

python - 从数字中过滤列表的最pythonic方法是什么?

java - 从映射列表中输出键/值对

java - 具有生成 com.sun.jersey.spi.inject.Errors 的复合(两部分)主键的 Glassfish/PostgreSQL 表

java - 下界通配符会在 javac 中引起问题,但不会在 Eclipse 中引起问题

java - 在 Tomcat 中找不到模块 'null'