Sky
Sky

Reputation: 301

How not ignore whitespace in java arraylist?

I have a java Package and within that package I have a java class called Items which stores some details of items and holds some methods. this is how my array list looks like:

 ArrayList<Items> itemList = new ArrayList<Items>();

With in my main program I have added details for the items which looks like this

itemList.add(new Items("A1","Samsung Galaxy S3", " Super AMOLED plus display is 4.8-inches making web browsing, watching videos or viewing photos a real joy");

itemList.add(new Items("A2","Apple mac pro", "2.2Ghz Core i7 Processor "); 

I have tried sending this to my GUI java client to a Textarea which looks like

output.println(item.getName()); 

but it seems to not appear as one string and part of the name string appears in another Jlist instead of the JTeaxtArea. I have tried doing it like this:

     itemList.toString.add(new Items("A1","Samsung Galaxy S3", " Super AMOLED plus display is 4.8-inches making web browsing, watching videos or viewing photos a real joy");

but seems to not recognize it as a string?

My item class looks like this:

public class items
{
    private String itemName;

    public Items(String itemName)
    {
        this.itemName = itemName;
    }
     public String getName()
    {
        return itemName;
    }
}

Upvotes: 0

Views: 191

Answers (1)

Kshitij
Kshitij

Reputation: 361

I don't think the code you've posted would compile. The Item class that you need is probably something like this:

public class Items {
    private String itemCode;
    private String itemName;
    private String itemDescription;

    public Items(String itemCode, String itemName, String itemDescription) {
      this.itemCode = itemCode;
      this.itemName = itemName;
      this.itemDescription = itemDescription;
    }

    public String getName() {
        return itemName;
    }

    public String getFullName() {
        return itemCode + " " + itemName + " " + itemDescription;
    }
}

Once you change the constructor to take three String argument, the following should start working:

itemList.add(new Items("A1","Samsung Galaxy S3", " Super AMOLED plus display is 4.8-inches making web browsing, watching videos or viewing photos a real joy");

itemList.add(new Items("A2","Apple mac pro", "2.2Ghz Core i7 Processor "); 

Try this out.

Upvotes: 1

Related Questions