0

I've been working on this for hours just as practice and everything should work perfectly as I've used another class and objects example as a template for this. when I put in the scanner "shirt", "M", and "400", this is what comes up.

Shirt
M
400
Shirt isShirt@f6f4d33

public class Shirt {

    private String shirtColor;
    private String size;
    private int price;

    public Shirt(String shirtColor, String size, int price) {

    }

    public void setColor(String shirtColor) {
        this.shirtColor = shirtColor;
    }

    public String getColor() {
        return this.shirtColor;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getSize() {
        return this.size;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPrice() {
        return this.price;
    }

}

and this is my main that I run the previous class from(my driver)

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String shirtColor;
        String size; 
        int price;

        shirtColor = scan.next();
        size = scan.next();
        price = scan.nextInt();

        Shirt shirter = new Shirt(shirtColor, size, price);
        System.out.print("Shirt is" + shirter);

    }
}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
zzzzz
  • 87
  • 6

3 Answers3

2

You need to implement a toString method:

public class Shirt {

    private String shirtColor;
    private String size;
    private int price;

    ...

    @Override
    public String toString() {
        return "shirtColor: " + shirtColor + ", size: " + size + ", price: " + price;
    }
}

As @SwaritAgarwal notices, you need to implement the constructor so that it sets all the arguments.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
1

It is printing Object class with its HashCode. If you are looking to print the value set in Shirt Object, override toString() method in your Shirt Class. Then, in your main, just print out shirter.toString().

Change your constructor as shown below:

public Shirt(String shirtColor, String size, int price) { 
   this.shirtColor=shirtColor; 
   this.size=size; 
   this.price=price 
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Swarit Agarwal
  • 2,520
  • 1
  • 26
  • 33
  • it just prints out null, null, and 0 – zzzzz Feb 27 '20 at 06:47
  • Have you tried after shirtColor = scan.next(); size = scan.next(); price = scan.nextInt(); Shirt shirter = new Shirt(shirtColor, size, price); System.out.print("Shirt is" + shirter.toString()); I believe – Swarit Agarwal Feb 27 '20 at 06:47
  • 1
    @simonshampoo Because your contructor has an empty body. You are in Java, all is hand job here, buddy :) – Honza Zidek Feb 27 '20 at 06:48
  • In your constructor public Shirt(String shirtColor, String size, int price) { this.shirtColor=shirtColor; this.size=size; this.price=price } – Swarit Agarwal Feb 27 '20 at 06:48
1

When you call

System.out.print("Shirt is" + shirter);

what happens is the following:

  1. Java compiler tries to "cast" the object shirter to String. I put "cast" in apostrophes as it is not a real type cast, but calling its method toString().

  2. The toString() method is inherited from the Object class (the parent class of all the classes). The default definition is that it returns the name of the class (in your case Shirt followed by its internal hash code.

  3. If you want to have your object nicely printable (and also nicely displayed in the debugger in your IDE etc.), you should redefine the toString() method in your Shirt class.

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118