0

I have to take user input of (int id, String title, String folder, int pages). I have to get the output in ascending order of string title(lexicographical). I have written the code, but i output is quite different.

package zzz;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;

class Book{
    int id;
    String title;
    String folder;
    int pages;
}


public class practice {

    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    Book b1 = new Book();
    Book b2 = new Book();
    Book b3 = new Book();
    Book b[]= {b1,b2,b3};
    for(int i=0;i<b.length;i++) {
        b[i].id=sc.nextInt();
        sc.nextLine();
    b[i].title=sc.next();
        sc.nextLine();
        b[i].folder=sc.next();
        b[i].pages=sc.nextInt();
    }
    Book temp = null;
    for(int i=0;i<b.length;i++) {
        for(int j=0;j<b.length-1-i;j++) {
            if(b[i].title.compareTo(b[j].title)<0) {
             temp =b[j];
            b[j]=b[j+1];
            b[j+1]=temp;
        }}
    }
    for(int i=0;i<b.length;i++) {
        System.out.println(b[i].id+" "+b[i].title+" "+b[i].folder+" "+b[i].pages);
    }

    }}

enter image description here

A_B
  • 29
  • 1
  • 8
  • Does this answer your question? [Implements Comparable to get alphabetical sort with Strings](https://stackoverflow.com/questions/22871583/implements-comparable-to-get-alphabetical-sort-with-strings) – Danyman Feb 08 '20 at 15:25
  • Does the Book class assign a default value to its title? – Bohemian Feb 08 '20 at 18:02
  • @Bohemian, no it doesn't – A_B Feb 08 '20 at 18:42
  • Then your problem is that title is null and you’re invoking the `compareTo()` method on a null object. – Bohemian Feb 08 '20 at 23:48

3 Answers3

2

I would remove the class at the beginning and add the class after the main method as:

static class Book implements Comparable<Book>{
    int id;
    String title;
    String folder;
    int pages;

    @Override
    public int compareTo(Book other) {
        //If this is backword then switch it to -> other.title.compareTo(this.title);
        return this.title.compareTo(other.title);
     }
}

Same as: Implements Comparable to get alphabetical sort with Strings

Then you can just get the array of books and use Arrays.sort(book_arr);

For you:

package zzz;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;


public class practice {

    public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    Book b1 = new Book();
    Book b2 = new Book();
    Book b3 = new Book();
    Book b[]= {b1,b2,b3};
    for(int i=0;i<b.length;i++) {
        b[i].id=sc.nextInt();
        sc.nextLine();
        b[i].title=sc.next();
        sc.nextLine();
        b[i].folder=sc.next();
        b[i].pages=sc.nextInt();
    }

    //Sort!
    Arrays.sort(b);

    for(int i=0;i<b.length;i++) {
        System.out.println(b[i].id+" "+b[i].title+" "+b[i].folder+" "+b[i].pages);
    }

    }
    static class Book implements Comparable<Book>{
        int id;
        String title;
        String folder;
        int pages;

        @Override
         public int compareTo(Book other) {
             return this.title.compareTo(other.title);
         }
     }
}

It works!

Danyman
  • 63
  • 1
  • 9
  • didn't get you, can you please amend my code and help me out – A_B Feb 08 '20 at 18:02
  • thanks a lot. i amended my code in a diff manner, but the output is coming different. it should come aa, cc, ss but its coming aa ss cc. please have a look – A_B Feb 08 '20 at 18:14
  • Fixed! Here is the new code. I have tested in eclipse and it has worked! – Danyman Feb 08 '20 at 22:03
0

You are getting the NullPointerException because b[i].title=sc.next(); is commented out.

A way of doing this without making Book Comparable is to sort a Stream:

Stream.of(b)
    .sorted((thisBook,anotherBook) -> thisBook.title.compareTo(anotherBook.title))
    .forEach(bk -> System.out.println(bk.id+" "+bk.title+" "+bk.folder+" "+bk.pages));
}
Dave The Dane
  • 650
  • 1
  • 7
  • 18
0

You can simply handle your problem using stream in java 8, so instead of using array, you can use List, then you must stream it, init it, and finally sort books based on their title.

In summery

1- Create Book objects (the same as yours)

2- Create Book list (Instead of an array)

3- Stream bookList, Init each book object then Sort them based on the title

4- Print the result

I coded your scenario as follows

public static void main(String[] args)  {

        //Create Book Objects
        Scanner sc = new Scanner(System.in);
        Book b1 = new Book();
        Book b2 = new Book();
        Book b3 = new Book();

        //Create BookList
        List<Book> bookList = new ArrayList<>();
        bookList.add(b1);
        bookList.add(b2);
        bookList.add(b3);

        //Stream bookList, Init Books, Sort them based on the title
        bookList.stream().peek(book -> {

            //Init book objects (id , folder , title , pages)
            book.id = sc.nextInt();
            sc.nextLine();
            book.folder = sc.next();
            sc.nextLine();
            book.title = sc.next();
            sc.nextLine();
            book.pages = sc.nextInt();          

        }).sorted(Comparator.comparing(book -> book.title)).collect(Collectors.toList());


        //Print results
        bookList.forEach(book -> {
            System.out.println(book.title);
        });
    }