Click here to Skip to main content
15,506,384 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to my code to be able to read the input from the user and use it to create a Book-object that will be placed in the array "books".

Thanks in advance!

What I have tried:

import java.util.Arrays;
import java.util.Scanner;

public class ReadBooks {

	public static void main(String[] args) {
		// Don't change
		Scanner input = new Scanner(System.in);
		Book[] books = new Book[2];

		// Fill the array with books whom information has been input by user
		for (int n = 0; n < books.length; n++) {
			System.out.print("Titel> ");
			input.nextLine();
			System.out.print("Inbunden (J/N)> ");
			input.nextLine();
		}

		// Don't change
		System.out.println(Arrays.toString(books));
		input.close();
	}

}

// No need to change
class Book {

	String title;
	boolean bound;

	Book(String title, boolean bound) {
		this.title = title;
		this.bound = bound;
	}

	public int getPrice() {
		return bound ? 300 : 100;
	}

	@Override
	public String toString() {
		return title + " " + getPrice() + " " + bound;
	}

}
Posted
Updated 17-Jun-21 11:25am
v4

1 solution

Try adding this at the end of the loop:
Java
books[n] = bk;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900