Home > database >  dont shows the values in the array - Java
dont shows the values in the array - Java

Time:09-22

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

CodePudding user response:

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;

CodePudding user response:

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.

CodePudding user response:

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));

}
  • Related