0

i'm doing an exercise where i have that to store objects in the array, the problem is when i'm going to print the objects of the array, dont shows the values of type String, the output is:

AFGH
3
5

2
3

5
5

1
2

5
7

my code is:

public static void main(String[] args){
        
        Bill[] billsList = new Bill[5];
        Scanner scanner = new Scanner(System.in);
        String productCode = " ";
        int kilos = 0;
        int price = 0;
        
        for(int i = 0; i < billsList.length; i++) {
            System.out.println("Digit the code of the product: ");
            productCode = scanner.nextLine();
            scanner.nextLine();
            System.out.println("Digit the kilos sold: ");
            kilos = scanner.nextInt();
            System.out.println("Digit the price: ");
            price = scanner.nextInt();
            
            Bill bills = new Bill(productCode,kilos,price);
            
            billsList[i] = bills;
        }
        
        for(int i = 0; i < billsList.length; i++) {
            System.out.println(billsList[i]);
            System.out.println(billsList[i]);
            System.out.println(billsList[i]);
        }
        
}    

the code of the class Bill is this:

public class Bill {
    private String productCode;
    private int kilosSold;
    private int price;
    
    public Bill(String productCode,int kilosSold,int price) {
        this.productCode = productCode;
        this.kilosSold = kilosSold;
        this.price = price;
    }
    
}

3 Answers 3

1

In Bill class, you have to override toString() method of the Object class.

Somewhat like:

@Override
public String toString() {
   return "Product : " + productCode + " kilosold : " + kilosSold + "price : " + price;
}

or optionally you can use the @ToString of the lombok to avoid this boilerplate code.


Also, you can change your data members of class to final.

Somewhat like:

private final String productCode;
private final int kilosSold;
private final int price;
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, @Jose Erasmo Hernandez Yate. Please let me know if you face any issues with it. And if it solves your problem, please mark it accepted to help our developer around.
0

remove the nextLine and it would print fine:

System.out.println("Digit the code of the product: ");
productCode = scanner.next();
System.out.println("Digit the kilos sold: ");
 kilos = scanner.nextInt();

in addition, you should override the toString method like what was answered above.

Comments

0

If you want to print a complete object array, and all the attributes you should use the Arrays class method "toString". If you only want one of the attributes, for example: the product code, you should use a getter method. Investigate about getter and setter methods, it will help you a lot.

   public static void main(String[] args){

    Bill[] billsList = new Bill[5];
    Scanner scanner = new Scanner(System.in);
    String productCode = " ";
    int kilos = 0;
    int price = 0;

    for(int i = 0; i < billsList.length; i++) {
        System.out.println("Digit the code of the product: ");
        productCode = scanner.nextLine();
        System.out.println("Digit the kilos sold: ");
        kilos = scanner.nextInt();
        System.out.println("Digit the price: ");
        price = scanner.nextInt();

        Bill bills = new Bill(productCode,kilos,price);

        billsList[i] = bills;
    }

    System.out.println(Arrays.toString(billsList));

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.