Constructing an object from a string - java

Is it possible to construct an object given a string, toString() method, and the Class itself.
For example we have class Book.
class Book
{
// ...
String getTitle()
{
return title;
}
String getPubYear()
{
return pubYear;
}
void setTitle(String _title)
{
title = _title;
}
void setPubYear(String _pubYear)
{
pubYear = _pubYear;
}
public String toString(){
return title+" "+pubYear;
}
}
If we have the String:
"ExampleTitle 2017"
How can we create an instance of the class Book, with which has attribute:
title=ExampleTitle
pubyear=2017
We can do the following:
Book book = new Book();
String exampleString = "ExampleTitle 2017";
String[] parts = exampleString.split();
book.setTitle(parts[0]);
book.setPubYear(parts[1]);
But this is long winded. Is there a more automatic way to do this?

You can add a new constructor:
public Book(String s) {
String[] parts = s.split(" ");
if (parts.length() == 1) {
this.title = s;
} else {
this.title=parts[0];
this.pubYear(Integer.parseInt(parts[1]));
}
}
You should add NumberFormatException handling on your own but I recommend my post about it.
The above constructor will take a String, split it by a space and perform what you have done. You can use it like:
Book book = new Book("Example_title 2001");
but it's not the best approach. You should rather use standard patterns and first extract the values you want to set to your Book and then pass them to the constructor.
The good way of doing what you want will be to make another constructor:
public class Book {
public Book(String s, int y) {
this.title = s;
this.year = y;
}
}
Please change year field to int or long. Year shouldn't be a String as long as you're not using Roman numbers.

Related

How to Sort Objects from ArrayList

Background information: I have a text file of books filled with their respected information (ex: title, publisher, pageCount). I have successfully created an inheritance hierarchy with all the correct implementations, get/setters, and toString methods. The inheritance hierarchy essentially looks like this (code will be provided further below):
Book
title
publisher
pageCount
FictionBook inherits Book
author
genre
NonFictionBook inherits Book
language
Dictionary inherits NonFictionBook
versionNumber
CookBook inherits NonFictionBook
topic
Novel inherits FictionBook
isPartOfASeries (i.e. Y or N)
GraphicNovel inherits FictionBook
illustrator
The text file looks like this:
My problem: I have been following this example: https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/ but I do not fully understand how to use the compareTo method and further accurately sort the info into the correct classes. In my current code, my compareTo method seems to be printing the whole string and not accurately sorting it. I will provide all related code, output, and the parent class for the inheritance hierarchy class for better understanding.
My Question: How do I use the compareTo and collections.sort methods to accurately sort and print out my data. I have been stuck on this for a while so any guidance to me solving and learning this is appreciated!
Book Class (The Parent Class w/ Comparator and compare method):
public abstract class Book implements Comparable {
public String title;
public String publisher;
public int pageCount;
public Book(String title, String publisher, int pageCount) {
this.title = title;
this.publisher = publisher;
this.pageCount = pageCount;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public static Comparator<Book> bookTitleComp = new Comparator<Book>() {
public int compare(Book b1, Book b2) {
String bookTitle1 = b1.getTitle().toUpperCase();
String bookTitle2 = b2.getTitle().toUpperCase();
return bookTitle1.compareTo(bookTitle2);
}
};
// #Override
public String toString() {
return "Book " + "title: " + title + ", publisher: " + publisher + ", pageCount: " + pageCount;
}
}
Main Class (Here I am reading in my text file, sorting by the first number in the text field, so 1 = Dictionary, 2=Cookbook, 3=Novel, 4=GraphicNovel, and trying to print my compare method: last 4 lines)
ArrayList<Book> library = new ArrayList<Book>(); //Initialize ArrayList library
//Read text file in
FileReader fr = null;
try {
fr = new FileReader("library.txt"); //Reads in text file
} catch (FileNotFoundException ex) {
Logger.getLogger(GUICommandFunctions.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader reader = new BufferedReader(fr);
try {
String line;
while((line=reader.readLine())!=null) {
System.out.println(line);
String[] splitLine = line.split(", ");
if("1".equals(splitLine[0])) {
library.add(new Dictionary(splitLine[1], splitLine[2], Integer.parseInt(splitLine[3]), splitLine[4], splitLine[5]));
} else if ("2".equals(splitLine[0])) {
library.add(new Cookbook(splitLine[1], splitLine[2], Integer.parseInt(splitLine[3]), splitLine[4], splitLine[5]));
} else if ("3".equals(splitLine[0])) {
library.add(new Novel(splitLine[1], splitLine[2], Integer.parseInt(splitLine[3]), splitLine[4], splitLine[5], splitLine[6]));
}else if ("4".equals(splitLine[0])) {
library.add(new GraphicNovel(splitLine[1], splitLine[2], Integer.parseInt(splitLine[3]), splitLine[4], splitLine[5], splitLine[6]));
}
}
} catch (IOException ex) {
Logger.getLogger(GUICommandFunctions.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Book title sorting: ");
Collections.sort(library, Book.bookTitleComp);
for(Book str: library) {
System.out.println(str);
}
Current Output:
Book title sorting:
Novel isPartOfSeries: N
Novel isPartOfSeries: N
Cookbook topic: French Cooking
GraphicNovel illustrator: Neil Gaiman
Cookbook topic: Asian Cooking
Novel isPartOfSeries: Y
Novel isPartOfSeries: Y
Cookbook topic: Keto Cooking
Novel isPartOfSeries: Y
Dictionary version number: 4
Dictionary version number: 2
GraphicNovel illustrator: Dave Gibbons
GraphicNovel illustrator: Pia Guerra
BUILD SUCCESSFUL (total time: 3 seconds)
follow the below example
import java.util.*;
public class CustomObject {
private String customProperty;
public CustomObject(String property) {
this.customProperty = property;
}
public String getCustomProperty() {
return this.customProperty;
}
public static void main(String[] args) {
ArrayList<Customobject> list = new ArrayList<>();
list.add(new CustomObject("Z"));
list.add(new CustomObject("A"));
list.add(new CustomObject("B"));
list.add(new CustomObject("X"));
list.add(new CustomObject("Aa"));
list.sort((o1, o2) -> o1.getCustomProperty().compareTo(o2.getCustomProperty()));
for (CustomObject obj : list) {
System.out.println(obj.getCustomProperty());
}
}
}

search an array in java and print the output

In this java program I am trying to search if an ISBN number exists in the "Book" type array or not. But when I try to implement the method and display the output, I am getting an error which says " The method searchBook(Book, String) is undefined for the type Book". The <-- HERE comment shows where the error pops up. I do not understand how to rectify this error and any help will be appreciated. Thank you.
The driver class is : QuizMain
public class User {
int ID;
String name;
String email;
int age;
String isbn;
void searchBook(Book[] b, String isbn) {
for (int i =0;i<6;i++) {
if (b[i].ISBN == isbn) {
System.out.println(b[i].title);
} else {
System.out.println("ISBN Not Found");
}
}
}
}
public class Book {
String title;
String author;
String ISBN;
float rating;
int noOfDays;
void displayBookDetails() {
System.out.println("Title\tAuthor\tISBN\tRating"+this.title +this.author + this.ISBN +this.rating);
}
// book constructor
public Book(String title, String author, String ISBN, float rating) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
this.rating = rating;
}
}
public class QuizMain {
public static void main(String[] args) {
Book[] arr = new Book[6];
arr[0] = new Book("Vincent la la ","king","2194-5357",6.5f);
arr[1] = new Book("A man of wisdom","henry","2193-4567",3.2f);
arr[2] = new Book("Apple Garden","timorthy","2104-3080",1.2f);
arr[3] = new Book("Sherlock","Arthur","2165-0932",5.5f);
arr[4] = new Book("Hello John","Tarnia","2134-2342",1.5f);
arr[5] = new Book("Tarzan","Martin","2111-0564",4.2f);
for(int i =0;i<arr.length;i++) {
arr[i].searchBook(arr[i], "2165-0932"); // <-- HERE
}
arr.searchBook(arr[5], "2165-0932"); // <-- HERE
}
}
In the User class the searchBook(Book[] b, String isbn) method accepts an array of Book and a String isbn value.
But while calling this method you are passing only one Book object instead of array of Book.
Move the searchBook implementation to QuizMain class and call this by passing array of Book as shown below for a workaround .
searchBook(arr, "2165-0932");
May be you need to concentrate on designing the class and its behavior.
There are two problems in your code. First one your searchBook function expecting Book type array and string but you are passing it one Book instant i.e arr[i] with string and secondly you have define the searchBook() function in User class and using it with object of Book class. Move your function to Book class. Even you don't need that function in any of Book or User class you can simply define static function searchBook(Book[] books, String isbn) inside your QuizMain class and call it like
searchBook(arr,"8344-3452")

Find repeated elements in array list

The following code is suppose to take a string for author and title to check if that exact book is in the array list if it is then it would return the number of copies in the array. So far it's only checking if the book is in the array list, but I was wondering if there is any IPA that I can use to find repeated elements in an array list
public class Book{
private title;
private author;
}
public class Library {
private ArrayList<Book>libraryBooks;
public int checkNumCopies(String title,String author){
int numBookCopies = 0;
for(Book b:libraryBooks){
if((b.equals(title))&& (b.equals(author))){
return "Book is in the library";
}
else
return "Book is not in the library";
}
}
With proper implementation of equals and hashcode implementation in Book
class Book {
private String author;
private String title;
//Getters and Setters
//hashCode and equals impl
//toString impl
}
this can be achieved using streams
List<Book> books = Arrays.asList(new Book("book-1", "title-1"), new Book("book-2", "title-2"), new Book("book-3", "title-3"),
new Book("book-1", "title-1"), new Book("book-2", "title-2"));
Map<Book, Long> bookCount = books.stream().collect(Collectors.groupingBy(b -> b, Collectors.counting()));
System.out.println(bookCount);
output
{Book [author=book-3, title=title-3]=1, Book [author=book-2, title=title-2]=2, Book [author=book-1, title=title-1]=2}
Your Book fields are missing types (I assume you wanted Strings), and you have no accessor (or getters) for those fields. Also, I would probably override equals and use that. Something like,
public class Book {
public Book(String title, String author) {
this.title = title;
this.author = author;
}
private String title;
private String author;
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Book) {
Book b = (Book) obj;
return b.title.equals(title) && b.author.equals(author);
}
return false;
}
}
And then to use it, I would do something like
private List<Book> libraryBooks = new ArrayList<>();
public int checkNumCopies(String title, String author) {
Book toFind = new Book(title, author);
int numBookCopies = 0;
for (Book b : libraryBooks) {
if (b.equals(toFind)) {
numBookCopies++;
}
}
return numBookCopies;
}
or in Java 8+, like
Book toFind = new Book(title, author);
return (int) libraryBooks.stream().filter(book -> book.equals(toFind)).count();
Or you could create an equals method on Book and then use a HashSet. Something like
public class Book{
private title;
private author;
//implement hashcode
public boolean equals(Object o) {
if (!(o instanceof Book)){ return false}
Book that = Book.class.cast(o);
// you may want to do a null check here
return this.title.equals(that.title) && this.author.equals(that.author);
}
}
public class Library {
private ArrayList<Book>libraryBooks;
public int checkNumCopies(String title,String author){
return libraryBooks.size() - new HashSet(libraryBooks).size()
}
}
Im not sure entirely on what you need (sorry cant quite understand the question) but hopefully this at least gives you some ideas.
note: I havent compiled this so please excuse any compilation errors.
Here is the solution using java streams API
public class Library {
private ArrayList<Book> libraryBooks;
public long checkNumCopies(String title, String author) {
return libraryBooks
.stream()
.filter(book -> book.getAuthor().equals(author) && book.getTitle().equals(title))
.count();
}
}
I presupposed that there are getter methods in Book class.
If you want to find the number of copies of a book, that would be:
public int checkNumCopies(String title, String author) {
int numBookCopies = 0;
for (Book b : libraryBooks) {
if ((b.title.equals(title)) && (b.author.equals(author))) {
numBookCopies++;
}
}
return numBookCopies;
}
Otherwise, instead of sending title and author individually you can send a book object. That'd be better way :
public int checkNumCopies(Book book) {
int numBookCopies = 0;
for (Book b : libraryBooks) {
if ((b.title.equals(book.title)) && (b.author.equals(book.author))) {
numBookCopies++;
}
}
return numBookCopies;
}
Since you have made the fields private, you need to write getter methods for getting the values from the fields.

Appending a specific object in an arraylist using java?

What im trying to do is have the user input an index of "record" they want to append. The index will be used to look at a specific object in the array list. Everything stored in that object will be written to the input from. The user will then be able to append their input and write it back to the same object. What I'm stuck on is the taking data from the object and adding it to the form controls. I was am using
Movie genreOfMovie = (Movie) movieList.get(Integer.parseInt(index) - 1);
but that gave me object instance which is not a string that I wanted.
I was told to use
#Override
public String toString() {
return genreOfMovie;
}
It worked but only for that veritable, is there a way to make it work for all the variables in the selected object?
If you have a text field (for instance for the title of the movie) I suggest you add setTitle(String newTitle) and getTitle() to your Movie class.
With this in place, you can do
textField.setText(selectedMovie.getTitle());
and, when the user clicks save,
selectedMovie.setTitle(textField.getText());
If you have more properties (genre, director, ...) I suggest you add more get-/set-methods.
Unless the class has an very obvious String-representation, the toString should only be used in debugging purposes.
If you want to get all variables data from Movie object,
either
include a method inside Movie object which appends the variables data in required format and gives as String output.
or
Write your own method which will take Movie object as an input, and calls required fields getter methods like getTitle()+getGenreOfMovie+getLength() like that and return a String object.
And important thing is to check for Null or Empty data inside those variables.
import java.util.ArrayList;
import java.util.List;
public class MovieTest {
public static void main(String[] args) {
List<Movie> movies = new ArrayList<Movie>();
movies.add(new Movie("A"));
movies.add(new Movie("B"));
movies.add(new Movie("C"));
Movie movie = movies.get(1);
System.out.println(movie);
movie.setDirector("Director for B");
movie = movies.get(1);
System.out.println(movie);
}
}
class Movie {
String title;
String director;
int length;
Movie(String title) {
this.title = title;
}
public String getTitle() { return title; }
public String getDirector() { return director; }
public int getLength() { return length; }
public void setTitle(String title) { this.title=title; }
public void setDirector(String director) { this.director = director; }
public void setLength(int length) { this.length = length; }
public String toString() {
return "[" + title + "]["+ director +"][" + length + "]";
}
}

how can I return a String?

package book1;
import java.util.ArrayList;
public abstract class Book {
public String Book (String name, String ref_num, int owned_copies, int loaned_copies ){
return;
}
}
class Fiction extends Book{
public Fiction(String name, String ref_num, int owned_copies, String author) {
}
}
at the moment when i input values into the variable arguments and call them with this :
public static class BookTest {
public static void main(String[] args) {
ArrayList<Book> library = new ArrayList<Book>();
library.add(new Fiction("The Saga of An Aga","F001",3,"A.Stove"));
library.add(new Fiction("Dangerous Cliffs","F002",4,"Eileen Dover"));
for (Book b: library) System.out.println(b);
System.out.println();
}
}
i get a return value of this:
book1.Fiction#15db9742
book1.Fiction#6d06d69c
book1.NonFiction#7852e922
book1.ReferenceBook#4e25154f
how can i convert the classes to return a string value instead of the object value? I need to do this without changing BookTest class. I know i need to use to string to convert the values. but i don't know how to catch the return value with it. could someone please point me in the right direction on how to convert this output into a string value?
You need to overwrite the toString() Method of your Book class. In this class you can generate a String however you like. Example:
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.author).append(": ").append(this.title);
return sb.toString();
}
You need to override the toString() method in your Book or Fiction class. The method is actually declared in the Object class, which all classes inherit from.
#Override
public String toString(){
return ""; // Replace this String with the variables or String literals that you want to return and print.
}
This method is called by System.out.println() and System.out.print() when they receive an object in the parameter (as opposed to a primitive, such as int and float).
To reference the variables in the method, you'll need to declare them in the class and store them via the class's constructor.
For example:
public abstract class Book {
private String name;
private String reference;
private int ownedCopies;
private int loanedCopies;
public Book (String name, String reference, int ownedCopies, int loanedCopies) {
this.name = name;
this.reference = reference;
this.ownedCopies = ownedCopies;
this.loanedCopies = loanedCopies;
}
#Override
public String toString(){
return name + ", Ref:" + reference + ", OwnedCopies: " + ownedCopies + ", LoanedCopies: " + loanedCopies; // Replace this String with the variables or String literals that you want to return and print.
}
}
The classes you have defined, don't store any values. It is in other words useful to construct a new book. You need to provide fields:
public abstract class Book {
private String name;
private String ref_num;
private int owned_copies;
private int loaned_copies;
public String Book (String name, String ref_num, int owned_copies, int loaned_copies) {
this.name = name;
this.ref_num = ref_num;
this.owned_copies = owned_copies;
this.loaned_copies = loaned_copies;
}
public String getName () {
return name;
}
//other getters
}
Now an object is basically a set of fields. If you want to print something, you can access and print one of these fields, for instance:
for (Book b: library) System.out.println(b.getName());
In Java, you can also provide a default way to print an object by overriding the toString method:
#Override
public String toString () {
return ref_num+" "+name;
}
in the Book class.
Need to give your object Book a ToString() override.
http://www.javapractices.com/topic/TopicAction.do?Id=55
Example:
#Override public String toString()
{
return name;
}
Where name, is a string in the Class.
I am hoping that you have assigned the passed arguments to certain attributes of the classes. Now, once you are done with that, you can override the toString() method in Book to return your customized string for printing.

Categories

Resources