0

I have troubles with a school Java program could you give me a hand? The program should make you give him some book informations in input like title, price for each one. Now I created an Array of objects and each object should have a private variable for each option (to set with method setter and declare with method getter). The problem is that i don't know where is the mistake but the program print in output just the last variable which is the price. The other ones are all null. Could you help me please? main:

    class Esercizio2
{
    public static void main(String[] args)
    {
        // Objects
        ConsoleReader2 keyboard = new ConsoleReader2(System.in);
        // Init
        int n = 0;  // book number
        // temp variables
        String title;
        String writer;
        String house;
        String price;
        // Calc
        System.out.println("How many books you wanna analyze?");
        do
        {
            n = keyboard.readInt();
            if (n <= 0)
            {
                System.out.print("You've inserted a negative or 0 number, you have to input a positive number, try again ===> ");
            }
        }
        while (n <= 0);
        // array of the books
        Libri2[] info = new Libri2[n];
        // through arrays
        for (int i = 0, j = 1; i < info.length; i++, j++)
        {
            // input title
            System.out.print("Input the title for book number " + j + " ===> ");
            title = keyboard.readLine();
            info[i] = new Libri2();
            info[i].setTitle(title);
            // input writer
            System.out.print("Input the writer for book number " + j + " ===> ");
            writer = keyboard.readLine();
            info[i] = new Libri2();
            info[i].setWriter(writer);
            // input house
            System.out.print("Input the house for book number " + j + " ===> ");
            house = keyboard.readLine();
            info[i] = new Libri2();
            info[i].setHouse(house);
            // input price
            System.out.print("Input the price for book number " + j + " ===> ");
            price = keyboard.readLine();
            info[i] = new Libri2();
            info[i].setPrice(price);
        }
        // Declaration
        for (int i = 0, j = 1; i < info.length; i++, j++)
        {
            System.out.print("The title of book number " + j + " is  ===> " + info[i].getTitle() + "\n");
            System.out.println("His features are:\n" + info[i].getWriter() + "\n" + info[i].getHouse() + "\n" + info[i].getPrice());
        }
    }
}

ConsoleReader2 is a class which I use instead of Scanner but is the same thing, readLine is a method for input a String. class with setter and getter methods:

    public class Libri2
{
    // attributs:
    private String title;
    private String writer;
    private String house;
    private String price;
    // methods: (setter e getter)
    // give title
    public String getTitle()
    {
        return this.title;
    }
    // set title
    public void setTitle(String m)
    {
        this.title = m;
    }
    // give writer
    public String getWriter()
    {
        return this.writer;
    }
    // set writer
    public void setWriter(String m)
    {
        this.writer = m;
    }
    // give house
    public String getHouse()
    {
        return this.house;
    }
    // set house
    public void setHouse(String m)
    {
        this.house = m;
    }
    // give price
    public String getPrice()
    {
        return this.price;
    }
    // set price
    public void setPrice(String m)
    {
        this.price = m;
    }
}

2 Answers 2

4

You're recreating and resetting the current element each time you set a property, a la

info[i] = new Libri2();

Leaving you with an array of objects with only prices.

Sign up to request clarification or add additional context in comments.

Comments

0

put info[i]=new Libri2(); as first line of for loop and delete all other occurrences of info[i]=new Libri2();. The reason why you are getting wrong output is because each object in your Libri2[] array is getting re-initialised leaving you with last initialisation after each iteration i.e

price = keyboard.readLine(); info[i] = new Libri2(); info[i].setPrice(price);

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.