Java 如何修复只查看最后一个条目的FileInputStream?

Java 如何修复只查看最后一个条目的FileInputStream?,java,arraylist,fileinputstream,Java,Arraylist,Fileinputstream,我正在尝试制作一个我的游戏小组喜欢的纸牌游戏的简单版本,这样我们的残疾朋友就可以参加游戏之夜了。但是,当我尝试使用而(inputStream.hasNextLine())通过文本文件将卡数据值添加到ArrayList时,FileInputStream似乎只读取文本文件的最后一行,这给我带来了麻烦 我正在尝试将值打印为Card类中我的cardToString()方法中定义的字符串。代码当前打印文件中的最后一张卡,ID:71 信用卡号码:72 堆栈:门 类型:电源 名称:功率吸收 权力:排名3 文字

我正在尝试制作一个我的游戏小组喜欢的纸牌游戏的简单版本,这样我们的残疾朋友就可以参加游戏之夜了。但是,当我尝试使用
而(inputStream.hasNextLine())
通过文本文件将卡数据值添加到ArrayList时,FileInputStream似乎只读取文本文件的最后一行,这给我带来了麻烦

我正在尝试将值打印为Card类中我的
cardToString()
方法中定义的字符串。代码当前打印文件中的最后一张卡,
ID:71
信用卡号码:72
堆栈:门
类型:电源
名称:功率吸收
权力:排名3
文字:你可以丢弃一张牌,试图偷取另一玩家携带的异能。滚动模具;4或更高成功率。否则,你会被抓住并失去一个关卡。
奖金:对于所有72张卡,使用
cardToString()
方法获得3张

。第一个项目的名称应为“Gradydon Creed”,最后一个输出的名称应为“功率吸收”

任何帮助都将不胜感激

以下是我的主要方法类:

private static int stack, type, name, text, bad, level, treasure, pLevel, modifier, rank;

public static void main(String[] args) {  

Deck deck = new Deck();

try {
    Scanner inputStream = null;

    inputStream = new Scanner(new FileInputStream("MunchkinIDSheet.txt"));

    while(inputStream.hasNextLine()){
        stack = inputStream.nextInt();
        type = inputStream.nextInt();
        name = inputStream.nextInt();
        text = inputStream.nextInt();
        bad = inputStream.nextInt();
        level = inputStream.nextInt();
        treasure = inputStream.nextInt();
        pLevel = inputStream.nextInt();
        modifier = inputStream.nextInt();
        rank = inputStream.nextInt();

    deck.addCard(new Card(stack, type, name, text, bad, level, treasure, pLevel, modifier, rank));
    }

    inputStream.close();
}
catch (Exception e) {
    System.out.println(e);
}

deck.list();
}
我的甲板班:

import java.util.ArrayList;

public class Deck {
private static ArrayList<Card> deck = new ArrayList<>();

public void addCard(Card n){
    deck.add(n);
}

public static void list(){
    for(int i = 0; i < deck.size(); i++){
        System.out.println("ID: " + i + "\n" + "Card: " + (i + 1) + "\n" + Card.cardToString(deck.get(i)));
        System.out.println();
    }
  }
}
最后是文本文件中的数据值,保存为代码中的“MunchkinIDSheet.txt”:

1 1 0 0 0 0 0 1 0 0
1 1 1 1 1 1 1 10 0
1 1 2 2 2 2 2 1 0 0
1 1 3 3 3 3 3 1 0 0
1 1 4 4 4 4 4 1 0 0
1 1 5 5 5 5 5 1 0 0
1 1 6 6 6 6 6 1 0 0
1 1 7 7 7 7 7 1 0 0
1 1 8 8 8 8 8 1 0 0
1 1 9 9 9 9 9 1 0 0
1 1 10 10 10 10 10 1 0 0
1 1 11 11 11 11 11 1 0 0
1 1 12 12 12 12 12 1 0 0
1 1 13 13 13 13 13 1 0 0
1 1 14 14 14 14 14 1 0 0
1 1 15 15 15 15 15 1 0 0
1 1 16 16 16 16 16 1 0 0
1 1 17 17 17 17 17 1 0 0
1 1 18 18 18 18 18 1 0 0
1 1 19 19 19 19 19 1 0 0
1 1 20 20 20 20 20 1 0 0
1 1 21 21 21 21 21 1 0 0
1 1 22 22 22 22 22 1 0 0
1 1 23 23 23 23 23 1 0 0
1 1 24 24 24 24 24 1 0 0
1 1 25 25 25 25 25 2 0 0
1 1 26 26 26 26 26 2 0 0
1 1 27 27 27 27 27 2 0 0
1 1 28 28 28 28 28 2 0 0
1 2 29 29 -1 -1 -1 0 0 0
1 2 29 29 -1 -1 -1 0 0 0
1 2 31 31 -1 -1 -1 0 0 0
1 2 31 31 -1 -1 -1 0 0 0
1 2 33 33 -1 -1 -1 0 0 0
1 2 33 33 -1 -1 -1 0 0 0
1 3 35 35 -1 -1 -1 0 0 0
1 3 35 35 -1 -1 -1 0 0 0
1 3 35 35 -1 -1 -1 0 0 0
1 4 38 38 -1 -1 -1 0 0 0
1 4 38 38 -1 -1 -1 0 0 0
1 5 40 40 -1 -1 -1 0 10 0
1 5 41 41 -1 -1 -1 0 10 0
1 5 42 42 -1 -1 -1 0 5 0
1 5 43 43 -1 -1 -1 0 5 0
1 5 44 44 -1 -1 -1 0 -5 0
1 6 45 45 -1 -1 -1 0 0 0
1 14 46 46 -1 -1 -1 0 0 0
1 7 47 47 -1 -1 -1 0 0 0
1 7 48 48 -1 -1 -1 0 0 0
1 7 49 49 -1 -1 -1 0 0 0
1 7 50 50 -1 -1 -1 0 0 0
1 7 51 51 -1 -1 -1 0 0 0
1 7 52 52 -1 -1 -1 0 0 0
1 7 53 53 -1 -1 -1 0 0 0
1 7 54 54 -1 -1 -1 0 0 0
1 7 55 55 -1 -1 -1 0 0 0
1 7 56 56 -1 -1 -1 0 0 0
1 7 57 57 -1 -1 -1 0 0 0
1 7 58 58 -1 -1 -1 0 0 0
1 7 59 59 -1 -1 -1 0 0 0
1 8 60 60 -1 -1 -1 0 1 1
1 8 61 61 -1 -1 -1 0 1 1
1 8 62 62 -1 -1 -1 0 1 1
1 8 63 63 -1 -1 -1 0 1 1
1 8 64 64 -1 -1 -1 0 1 2
1 8 65 65 -1 -1 -1 0 2 2
1 8 66 66 -1 -1 -1 0 1 2
1 8 67 67 -1 -1 -1 0 2 2
1 8 68 68 -1 -1 -1 0 2 3
1 8 69 69 -1 -1 -1 0 4 3
1 8 70 70 -1 -1 -1 0 3 3
1 8 71 71 -1 -1 -1 0 3 3
这是一个很好的项目

您的问题是误用了
static
关键字。基本上,您将每个值作为
Card
类的属性,而不是单个
Card
实例。一个很好的解释它是如何工作的

此外,Java还内置了将对象表示为字符串的功能,
toString()
方法。如果你用它来代替Card.cardToString()
它会让你的生活更轻松

下面是一个
卡片
卡片组
的基本示例,可以为您指明正确的方向

// Card.java
public class Card {
    // An array containing information about cards in general should be static.
    private static String[] NAMES = new String[]{"Card 1", "Card 2"/*, ...*/};
    private static String[] TEXTS = new String[]{"Card 1 description", "Card 2 description"/*, ...*/};

    // These values, which are unique to each card, are not static.
    private int name, text; 

    public Card(int name, int text) {
        this.name = name;
        this.text = text;
    }

    public String toString() {
        // Instead of having cardToString(), we use this canonical function.
        return Card.NAMES[this.name] + ' ' + Card.TEXTS[this.description];
    }
}

// Deck.java
public class Deck {
    // Since there could be many decks, each with their own cards, this isn't static either.
    private List<Card> cards;

    public Deck() {
        this.cards = new ArrayList();
    }

    public void addCard(Card card) {
        this.cards.add(card);
    }

    public String toString() {
        String str = "";
        for (int i = 0; i < this.cards.size(); i++) {
            str += this.cards.get(i) + "\n";
        }
        return str;
    }
}

中的大多数变量不得为
静态
<代码>静态
变量绑定到类,但不绑定到特定实例
cardToString
也不应该是静态的,也不应该使用参数。我最初使用它时没有
static
关键字,但是我的
Deck.list()
方法返回时没有它,出现了一个错误,并且在几乎所有内容中都包含
static
之前无法解决。我是否缺少解决该问题的方法?在删除添加的
静态
关键字后,我在
cardToString()
方法的
Deck.list()部分上遇到的错误是:
非静态方法cardToString()无法从静态上下文中引用
@N.Spears-您需要返回到您的教科书/笔记/教程,阅读关于
静态
和非
静态
方法和字段之间的区别。您的“在几乎所有内容上添加静态”的方法并不能解决问题。当然,它消除了编译错误。。。但它给你的代码是行不通的。如果您理解了
静态
字段的含义,您就会理解为什么这意味着您的代码不能完成它需要做的事情。
// Card.java
public class Card {
    // An array containing information about cards in general should be static.
    private static String[] NAMES = new String[]{"Card 1", "Card 2"/*, ...*/};
    private static String[] TEXTS = new String[]{"Card 1 description", "Card 2 description"/*, ...*/};

    // These values, which are unique to each card, are not static.
    private int name, text; 

    public Card(int name, int text) {
        this.name = name;
        this.text = text;
    }

    public String toString() {
        // Instead of having cardToString(), we use this canonical function.
        return Card.NAMES[this.name] + ' ' + Card.TEXTS[this.description];
    }
}

// Deck.java
public class Deck {
    // Since there could be many decks, each with their own cards, this isn't static either.
    private List<Card> cards;

    public Deck() {
        this.cards = new ArrayList();
    }

    public void addCard(Card card) {
        this.cards.add(card);
    }

    public String toString() {
        String str = "";
        for (int i = 0; i < this.cards.size(); i++) {
            str += this.cards.get(i) + "\n";
        }
        return str;
    }
}
Deck deck = new Deck();
deck.addCard(new Card(0, 0));
// Etc...
System.out.println(deck);