简体   繁体   中英

Trying to display output from an ArrayList in JTextArea

I'm storing user input into an ArrayList of a class BarInfo. The class takes in multiple data: Strings, booleans, ints. Then I want to display the data stored in the order it was entered in a JTextArea. Now say the user inputs data for one bar with all the necessary information. When they display the data it works right. However, if more than one bar is inputted, or another bar is added afterwards, it displays multiple versions of the data with different data.

For example if two bars are entered the display will show both of the bars various times with different amounts of int data from both of them mixed, instead of just The first bar and then the second.

This is my class Bar:

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";
    }

} 

And my display code:

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

        }

I'm guessing my for loop is somehow off, but I'm not seeing it. Any help would be greatly appreciated.

UPDATE Output expected for say 2 bars:

The bar is located at: (barLoc)

The bar is named: (barName)

Live music: (true or false)

Food Service: (true or false)

Liquor currently in stock:

Vodka = (amount)

Whiskey = (amount)

Rum = (amount)

Gin = (amount)

Brandy = (amount)

Then the second bar with its data. What I'm getting is like 6-7 different bars from those two bar inputs.

UPDATE SAMPLE OUTPUT from 2 bars entered:

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

JTextArea.append() appends text , so it's added to what is there already. Since you also append jTextAreaDisplay.getText() you'll get duplicates if the loop runs more than once.

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM