尝试在JTextArea中显示ArrayList的输出

时间:2016-03-07 22:23:25

标签: java arraylist

我将用户输入存储到类BarInfo的ArrayList中。该类接收多个数据:字符串,布尔值,整数。然后我想显示按照在JTextArea中输入的顺序存储的数据。现在说用户输入一个条的数据,包含所有必要的信息。当他们显示数据时,它正常工作。但是,如果输入了多个条形,或者之后添加了另一个条形,则会显示具有不同数据的多个版本的数据。

例如,如果输入两个条形图,则显示将同时显示两个条形图,其中两个条形图的混合数据不同,而不仅仅是第一个条形图,然后是第二个条形图。

这是我的班级酒吧:

class BarInfo
{
    private final String barLoc, barName, v="Vodka", w="Whiskey", r="Rum", 
            g="Gin", b="Brandy"; 
    private final boolean music, food;
    private final int vCount, wCount, rCount, gCount, bCount;

    public BarInfo(String l, String n, boolean m, boolean f, int v, int w,
                    int r, int g, int b)
    {
        this.barLoc = l;
        this.barName = n;
        this.music = m;
        this.food = f;
        this.vCount = v;
        this.wCount = w;
        this.rCount = r;
        this.gCount = g;
        this.bCount = b;
    }

    @Override
    public String toString()
    {
        return "The bar is located at: " + barLoc + " \nThe bar is named: " + barName 
                + "\nLive Music: " + music + "\nFood Service: " + food + "\n"
                + "Liquor currently in stock (bottles):\n" + v + ": " +  vCount
                + "\n" + w + ": " + wCount + "\n" + r + ": " + rCount + "\n" 
                + g + ": " + gCount + "\n" + b + ": " + bCount + "\n\n";
    }

} 

我的显示代码:

for(int i=0; i<barList.size(); i++)
        {
            jTextAreaDisplay.append(jTextAreaDisplay.getText() 
                    + barList.get(i).toString() + "\n\n");

        }

我猜我的for循环是以某种方式关闭,但我没有看到它。任何帮助将不胜感激。

UPDATE 预期输出为2个柱:

  

酒吧位于:(barLoc)

     

该栏名为:(barName)

     

现场音乐:(真或假)

     

食品服务:(真或假)

     

现有的白酒:

     

伏特加=(金额)

     

威士忌=(金额)

     

朗姆酒=(金额)

     

Gin =(金额)

     

白兰地=(金额)

然后是第二个条形图及其数据。我得到的就是这两个条形输入中的6-7个不同的条形。

从输入的2个条形图更新样品输出:

The bar is located at: Kendall 
The bar is named: sgd
Live Music: false
Food Service: false
Liquor currently in stock (bottles):
Vodka: 10
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0



The bar is located at: Kendall 
The bar is named: sgd
Live Music: false
Food Service: false
Liquor currently in stock (bottles):
Vodka: 10
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0



The bar is located at: Kendall 
The bar is named: sgd
Live Music: false
Food Service: false
Liquor currently in stock (bottles):
Vodka: 10
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0



The bar is located at: Kendall 
The bar is named: sgd
Live Music: false
Food Service: false
Liquor currently in stock (bottles):
Vodka: 10
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0



The bar is located at: Kendall 
The bar is named: sgd
Live Music: false
Food Service: false
Liquor currently in stock (bottles):
Vodka: 10
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0



The bar is located at: Kendall 
The bar is named: sgd
Live Music: false
Food Service: false
Liquor currently in stock (bottles):
Vodka: 10
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0



The bar is located at: Miami Beach 
The bar is named: Wet
Live Music: true
Food Service: true
Liquor currently in stock (bottles):
Vodka: 20
Whiskey: 0
Rum: 0
Gin: 0
Brandy: 0

1 个答案:

答案 0 :(得分:1)

JTextArea.append() 附加文字,因此已将其添加到已存在的内容中。由于您还附加jTextAreaDisplay.getText(),如果循环运行多次,您将获得重复项。

尝试:

for(BarInfo b : barList) { // Java foreach loop
    jTextAreaDisplay.append(b.toString() + "\n\n");
}