Home > Net >  Having trouble iterating and printing Strings in ArrayList
Having trouble iterating and printing Strings in ArrayList

Time:04-26

I get results such as

device@3d4eac69 device@42a57993 device@75b84c92 device@6bc7c054 device@232204a1

From my understanding these numbers are the location of the data, but i want to actually print the Strings within the lists. I've been at this for awhile today and can't seem to figure it out. This is an attempt to make a prettier version of a 2d ArrayList program I had that did the job.

import java.util.ArrayList;



public class device {
private String sku;
private String name;
private boolean available;
static ArrayList<device> devices = new ArrayList<device>();

public device()
{
    
}

public device(String sku, String name, boolean isAvailable) 
{
    this.sku = sku; 
    this.name = name;
    this.available = isAvailable;
}

public String getStatusString() {
    
    return available ? "Available" : "Checked out";
}

public String devicePrint() 
{
    return String.format("SKU: %s, Name: %s, Status: %s", this.sku, this.name, this.getStatusString());
}




public void addDefaultDevices()
{
    devices.add(new device("6767A", "Apple 9.7-Inch iPad Pro", true));
    devices.add(new device("93P51B", "Amazon Kindle Fire Kids Edition", true));
    devices.add(new device("10N8C", "LeapFrog Epic Learning Tablet", true));
    devices.add(new device("85U20", "Amazon Kindle Fire HD 8", false));
    devices.add(new device("91H2D", "HP Envy Note 8", true));
    
}





import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MP2 {

static device deviceObj = new device();
static Scanner inputDevice = new Scanner(System.in);


public static void main(String[] args) {
    // TODO Auto-generated method stub
    

    deviceObj.addDefaultDevices();
    
    
    deviceObj.devices.forEach(System.out::println);
}

CodePudding user response:

You haven't defined a toString() method for the class device:

Either:

public String toString() {
    return devicePrint();
}

Or just replace devicePrint() with toString() (which is the conventional way):

public String toString() {
    return String.format("SKU: %s, Name: %s, Status: %s", this.sku, this.name, this.getStatusString());
}

Side note: Java naming convention has class names starting with capital letter, so class Device rather than class device.

And variable names are generally the camelcase version of the class name, ie:

static Device device = new Device();

CodePudding user response:

You could also solve your problems without much adjustment by using lambda.

deviceObj.devices.forEach((n) -> System.out::println(n.devicePrint()));

As it is shown here in the second example

I myself am relatively new to Java so all information without guarantee :-)

CodePudding user response:

Print the strings with help of " " operator instead of "%s".

public String toString() {
    return "SKU: "   this.sku   ", Name: "   this.name   ", Status: "   this.getStatusString();
}

In Java " " operator is used instead of "%s" (or format specifier) operator! And use toString method instead of "devicePrint()".

  •  Tags:  
  • java
  • Related