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);
}
}}