Solved

Insertion Sort Problem

Posted on 2013-05-19
17
350 Views
Last Modified: 2013-05-19
Hello experts

I am trying to add the insertion sort to my code and i have 3 errors

Here is the code:
import java.io.*;
import java.util.*;
import java.lang.*;


class UserInput {

public static String getString()
{
String line;
InputStreamReader eisodosString=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosString);
try {
line = br.readLine();
return line; 
}
catch (Exception e) {
return "Lathos" ; 
}
}

public static int getInteger() {
String line;
InputStreamReader eisodosInt=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosInt);
try{
line=br.readLine();
int i=Integer.parseInt(line);
return i;
}
catch(Exception e) {
return -1;
}
}

public static double getDouble() {
String line;
InputStreamReader eisodosDouble=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosDouble);
try{
line=br.readLine();
double i=Double.parseDouble(line);
return i;
}
catch(Exception e) {
return -1;
}
}

public static float getFloat() {

String line;
InputStreamReader eisodosFloat=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosFloat);
try{
line=br.readLine();
float i=Float.parseFloat(line);
return i;
}
catch(Exception e) {
return -1;
}
}
}

class Book {
	private String titlos;  // dhlwsh metablitwn ws private
	private String onoma;
	private String ISBN;
	private String oikos;
	private int selides;
	private int etos;
	private double timh;
	Book(String x,String y,String z,String a,int b,int c,double d){  //dhmiourgia domhth gia dhlwsh metablhtwn
		titlos=x;
		onoma=y;
		ISBN=z;
		oikos=a;
		selides=b;
		etos=c;
		timh=d;
		
	}
	public void settitlos(String titlosvi) {  // edw dhlwnontai oi set kai oi get methodoi
		titlos=titlosvi;
		}
		
	public String get_titlos() {
		return titlos;
	}
	
	public void setonoma(String onomasyg) {
		onoma=onomasyg;
		}

	public String get_onoma() {
		return onoma;
	}
	public void setISBN(String ISBN2) {
		ISBN=ISBN2;
		}

	public  String getISBN(){
		return ISBN;
	}
	public void setoikos(String Eoikos) {
		oikos=Eoikos;
		}

	public String get_oikos() {
		return oikos;
	}
	public void setselides(int Aselides) {
		selides=Aselides;
		}

	public int get_selides() {
		return selides;
	}
	public void setetos(int Eketos) {
		etos=Eketos;
		}

	public int get_etos(){
		return etos;
	}
	public void settimh(double timh2) {
		timh=timh2;
		}

	public double get_timh(){
		return timh;
	}
	
public String toString()   //dhmiourgia methodou toString gia tn emfanish tn parapanw parametrwn
   {
       return titlos+"     "+onoma+"     "+ISBN+"    "+oikos+"    "
               +selides+"     "+etos+"     "+timh+"     "; 
   }
}


class MyUtils {
	public static int seqSearch(Book [] books,int key){  //methodos seiriakhs anazitisis
		for(int i=0;i<books.length;i++)
			if(books[i].get_etos()==key)
				return i;
		return -1;
	}
	public static int seqSearch(Book [] books,String key){
		for(int i=0;i<books.length;i++)
			if(books[i].getISBN().equals(key))
				return i;
		return -1;
     }
	public static int search(Book [] books,int key){
		int left=0,right=books.length-1;
		return binSearch(books,key,left,right);
	}
	public static int binSearch(Book [] books,int key,int left,int right){  //methodos duadikhs anazitisis
		int mid,pos=-1;
		while(pos==-1 && left<=right){
			mid=(left+right)/2;
			if(key<books[mid].get_etos())
				right=mid-1;
			else
				if(key>books[mid].get_etos())
					left=mid+1;
				else
					pos=mid;
		}
		return pos;
		}
	public static int search(Book [] books,String key){
		int left=0,right=books.length-1;
		return binSearch(books,key,left,right);
	}
	public static int binSearch(Book [] books,String key,int left,int right){
		int mid,pos=-1;
		while(pos==-1 && left<=right){
			mid=(left+right)/2;
			if(key.compareTo(books[mid].getISBN())<0)
				right=mid-1;
			else
				if(key.compareTo(books[mid].get_oikos())>0)
					left=mid+1;
				else
					pos=mid;
		}
		return pos;
	}


	//arxh anazitisis value metaksi dio timwn
    public static void valueSearch(Book[] books, double start, double end) {
 System.out.println();
        System.out.println("Titlos Vivliou" + "    " + "Siggrafeas" + "          " + " ISBN" + "    " + " Ekdotikos Oikos" + "  Selides" + "  " + " Etos" + "  " + "   Timh");
 System.out.println();
 for (int i = 0; i < books.length; i++) {
           if (books[i] != null) { 
   if (books[i].get_timh() >= start && books[i].get_timh() <= end) {
                 System.out.println(books[i]);
   System.out.println();
            } else {
                 System.out.println("TO vivlio sto keli " + i + " vrisketai ektos epi9umhtwn oriwn ");
   System.out.println();
            } //telos else
        } //telos if arxiki
    }//telos for
}//telos anazitisis value


  //arxh algori9mos taksinomisis bubleSort
		public static void bubblesort (Book[] books) {
 		int i, j ;
	 		double temp;
                        String key;
		 boolean flag=true;
 		for (i=1; i < books.length; i++) {
  			flag=true;
  		for (j=0; j < books.length-i; j++) {
    		if (books[j].get_timh() > books[j+1].get_timh()) { 
    		temp=books[j].get_timh();
     		books[j].settimh(books[j+1].get_timh());
     		books[j+1].settimh(temp);
     		flag=false;
   		 
	
                 if (books[j].get_etos() > books[j+1].get_etos()) { 
    		temp=books[j].get_etos();
     		books[j].setetos(books[j+1].get_etos());
     		books[j+1].setetos((int) temp);
     		flag=false;
   		 
                 }
                }
                
                
   		if (flag) {
                 }
 		
                }
}
                }
                
        
              public static void inssort (Book[] books) {
		for(int i=1; i < books.length; i++) {
		int key = books[i];
		int position=i;
		// shift larger values to the right
		while(position>0 && books[position-1] > key) {
		books[position] = books[position-1];
		position--;
		}
		books[position] = key;
		}
		} 
		} 

			

		
	




class TestBook {
	static int thesh=0;
	public static void main(String[] args)
	throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		Book [] books=new Book[10];
		String ISBN2 = null;
		int epilogh=0;
		int epilogh2;
		int epilogh3;
		int epilogh4;
		int epilogh5;
		int i=0;
 		int EtosAnaz;
		menou:
		while(epilogh!=5){
		System.out.println("Lista Epilogwn: ");
		System.out.println("1.\tEisagwgh stoixeiwn vivliou");
		System.out.println("2.\tAnazhthsh vivliou");
		System.out.println("3.\tEmfanish stoixeiwn vivliwn me timh metaxu kapoiwn oriwn");
		System.out.println("4.\tTaksinomhsh Vivliwn");
		System.out.println("5.\tEmfanish stoixeiwn olwn twn vivliwn");
		System.out.println("6.\tTelos");
		System.out.println();
		do{
		System.out.println("Dwste Epilogh(1-4): ");
		epilogh=UserInput.getInteger();
		}while(epilogh<1 && epilogh>5);
		switch(epilogh){
		case 1:  //dhmiourgia prwtis epiloghs
			boolean gem=checkArray(books,thesh);
			if(gem) break;
			boolean eis=true;
			if(i<books.length && eis==true){
			System.out.print("Dose ton titlo tou vivliou: ");
			String titlos=UserInput.getString();
			System.out.print("Dose to onoma tou siggrafea: ");
			String onoma_s=UserInput.getString();
			System.out.print("Dose ton ekdotiko oiko: ");
			String oikos=UserInput.getString();
			System.out.print("Dose ton arithmo selidwn: ");
			int selides=UserInput.getInteger();
			System.out.print("Dose to etos ekdoshs: ");
			int etos=UserInput.getInteger();
			System.out.print("Dose thn timh: ");
			double timh=UserInput.getDouble();
			books[thesh]=new Book(titlos,onoma_s,getISBN(),oikos,selides,etos,timh);
			eis=false;
			i++;
			break;
			}
		//}
		 case 2:  //dhmiourgia deuteris epilogis
   				System.out.println("\tEpilogh Pediou Anazhthshs");
  			 System.out.println("1.ISBN");
   				System.out.println("2.Etos kikloforias");
   				System.out.println("3.Epistrofh sthn Arxikh Lista Epilogwn");
   				System.out.println();
   				do{  //eksasfalisi oti tha dwthei epilogi apo 1-5
   				System.out.println("Doste Epilogh(1-3): ");
   			epilogh2=UserInput.getInteger();
   			}while(epilogh2<1 && epilogh2>3);
   			if(epilogh2==1 || epilogh2==2){
   			 System.out.println("\tEpilogh Methodou Anazhthshs");
    				System.out.println("1.Seiriakh Anazhthsh");
    			System.out.println("2.Diadikh Anazhthsh");
    				System.out.println("3.Epistrofh Sthn Epilogh Pediou Anazhthshs");
    				System.out.println();
   				 do{  // eksasfalisi oti tha dwthei epilogi apo 1-3
   				 System.out.println("Dose Epilogh(1-3)");
    			epilogh3=UserInput.getInteger();
     			  }while(epilogh3>1 && epilogh3<3);
   			 for( i=0;i<books.length;i++){
  			  if(books[i]==null){
    		 System.out.println("O Pinakas den einai gematos");
   		  break;
   				 }
  	 		 }
   			 if(epilogh2==1 && epilogh3==1){
    				 System.out.println("Dwse ISBN gia na ginei anazitisi");
    			 ISBN2 = getISBN();
    				 System.out.println(MyUtils.seqSearch(books,ISBN2));
    			}
    			 else if(epilogh2==1 && epilogh3==2){
     			 System.out.println("dwse isbn gia na ginei anazitisi");
     				 ISBN2 = getISBN();
     				 System.out.println(MyUtils.search(books,ISBN2));
   			 }
    		 		else if(epilogh2==1 && epilogh3==3)
     			 continue menou;
     
    			 else if(epilogh2==2 && epilogh3==1){
    			 System.out.println("dwse etos gia na ginei anazitisi");
     			 EtosAnaz = UserInput.getInteger();
     			 System.out.println(MyUtils.seqSearch(books,EtosAnaz));
    			 }
    			 else if(epilogh2==2 && epilogh3==2){
     			 System.out.println("dwse etos gia na ginei anazitisi");
     			 EtosAnaz = UserInput.getInteger();
     			 System.out.println(MyUtils.search(books,EtosAnaz));
    				 }
    			 else if(epilogh2==2 && epilogh3==3){
     			 continue menou;
     			}
      
    			 break;
   			}
		case 3:  // dhmiourgia epiloghs 3
			for(i=0;i<books.length;i++){
				if(books[i]==null){
					System.out.println("O Pinakas den einai gematos");
					break;
				}
				}
			System.out.print("Dose ta oria: ");
			double p=Double.parseDouble(br.readLine());
			double a=Double.parseDouble(br.readLine());
			MyUtils.valueSearch(books,p, a);
		break;

case 4: //dhmiourgis epiloghs 4
			System.out.println("\tEpilogh Pediou Anazhthshs");
  			 System.out.println("1.Onoma Syggrafea");
   				System.out.println("2.ISBN");
   				System.out.println("3.Etos Kukloforias");
				System.out.println("4.Timh Vivliou");
				System.out.println("5.Epistrofh sthn Arxikh Lista Epilogwn");
   				System.out.println();
			do{  //eksasfalisi oti tha dwthei epilogi apo 1-5
   				System.out.println("Doste Epilogh(1-5): ");
				epilogh4=UserInput.getInteger();
				}while(epilogh4<1 && epilogh4>5);
   			if(epilogh4==1 || epilogh4==2){
			System.out.println("\tEpilogh Methodou");
    				System.out.println("1.Bubblesort");
    				System.out.println("2.Taksinomhsh me Eisagwgh");
    				System.out.println("3.Taksinomhsh me epilogh");
				System.out.println("4.Grigorh Taksinomhsh");
				System.out.println("5.Taksinomhsh me Sygxwneysh");
				System.out.println("6.Epistrofh sthn Epilogh Pediou Taksinomhshs");
    				System.out.println();
			do{  // eksasfalisi oti tha dwthei epilogi apo 1-5
   				 System.out.println("Dose Epilogh(1-5)");
				epilogh5=UserInput.getInteger();
     			  }while(epilogh5>1 && epilogh5<5);
   			 for( i=0;i<books.length;i++){
  			  if(books[i]==null){
    		 System.out.println("O Pinakas den einai gematos");
   		  break;
				}
			}
			if(epilogh4==1 && epilogh5==1){
    				 System.out.println("Dwse Timh Vivliou gia na ginei anazitisi");
                            int timh2 = UserInput.getInteger();
    				 MyUtils.bubblesort(books);
    			}
                        if(epilogh4==2 && epilogh5==1) {
                                System.out.println("Dwse etos gia na ginei anazitisi");
                            int Eketos = UserInput.getInteger();
    				 MyUtils.bubblesort(books);
    			 break;
   			}
                        }


		case 5:  // dhmiourgia epiloghs 4
			for( i=0;i<books.length;i++)
				System.out.println(books[i]);
		break;
		}
	}
	}
	public static boolean checkArray(Book books[],int thesh){
		boolean checks=false;
		if(thesh>=10){
			checks=true;
		}
		return checks;
	}
	public static boolean checkISBN(String ISBN1){
			if(ISBN1.length()==10){
				char p1=ISBN1.charAt(0);
				int ap=p1*10;
				char p2=ISBN1.charAt(1);
				int ap2=p2*9;
				char p3=ISBN1.charAt(2);
				int ap3=p3*8;
				char p4=ISBN1.charAt(3);
				int ap4=p4*7;
				char p5=ISBN1.charAt(4);
				int ap5=p5*6;
				char p6=ISBN1.charAt(5);
				int ap6=p6*5;
				char p7=ISBN1.charAt(6);
				int ap7=p7*4;
				char p8=ISBN1.charAt(7);
				int ap8=p8*3;
				char p9=ISBN1.charAt(8);
				int ap9=p9*2;
				char p10=ISBN1.charAt(9);
				int ap10=p10*1;
				int athroisma=ap+ap2+ap3+ap4+ap5+ap6+ap7+ap8+ap9+ap10;
				if(athroisma%11==0)
					return true;
			}
			else if(ISBN1.length()==13){
				char ps1=ISBN1.charAt(0);
				int apot=ps1*1;
				char ps2=ISBN1.charAt(1);
				int apot2=ps2*3;
				char ps3=ISBN1.charAt(2);
				int apot3=ps3*1;
				char ps4=ISBN1.charAt(3);
				int apot4=ps4*3;
				char ps5=ISBN1.charAt(4);
				int apot5=ps5*1;
				char ps6=ISBN1.charAt(5);
				int apot6=ps6*3;
				char ps7=ISBN1.charAt(6);
				int apot7=ps7*1;
				char ps8=ISBN1.charAt(7);
				int apot8=ps8*3;
				char ps9=ISBN1.charAt(8);
				int apot9=ps9*1;
				char ps10=ISBN1.charAt(9);
				int apot10=ps10*3;
				char ps11=ISBN1.charAt(10);
				int apot11=ps11*1;
				char ps12=ISBN1.charAt(11);
				int apot12=ps12*3;
				char ps13=ISBN1.charAt(12);
				int apot13=ps13*1;
				int athroisma1=apot+apot2+apot3+apot4+apot5+apot6+apot7+apot8+apot9+apot10+apot11+apot12+apot13;
				if(athroisma1%10==0)
					return true;
			else
				return false;	
		}
		return false;
	}
         public static String getISBN()   //methodos g n pairnoume tn isbn
 		throws IOException{
 		 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 		 String ISBN1 = " " ;
  			boolean ele;
  			do{
      System.out.print("Dose ton arithmo ISBN: ");
      ISBN1=UserInput.getString();
      ele=checkISBN(ISBN1);
      }while(ele!=true);
  return ISBN1;
  
 }
}

Open in new window


and the errors

Book2.java:249: error: incompatible types
                int key = books[i];
                               ^
  required: int
  found:    Book
Book2.java:252: error: bad operand types for binary operator '>'
                while(position>0 && books[position-1] > key) {
                                                      ^
  first type:  Book
  second type: int
Book2.java:256: error: incompatible types
                books[position] = key;
                                  ^
  required: Book
  found:    int
3 errors

Open in new window

0
Comment
Question by:Panos
  • 10
  • 4
  • 3
17 Comments
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Book is an Object and int is not. 'etos' and 'selides' are your only members of Book which are ints - do you mean to collect one of them?
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Hi krakatoa

You have right that "etos" and "selides" are the only members of Book which are ints.I would to collect only "etos" and not selides in inssort.
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
I made few changes ande now i have 1 error:

 
public static void inssort (Book[] books) {
		for(int i=1; i < books.length; i++) {
		int key = books[i].get_etos();
		int position=i;
		// shift larger values to the right
		while(position>0 && books[position-1].get_etos() > key) {
		books[position].setetos (books[position-1].get_etos());
		position--;
		}
		books[position].setetos = key;
		}
		} 

Open in new window


This is the error:
Book2.java:256: error: cannot find symbol
                books[position].setetos = key;
                               ^
  symbol:   variable setetos
  location: class Book
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
I solved the errors but now when i run the program and i give eg:"ISBN=0123456789" it is showing me
this:
Exception in thread "main" java.lang.NullPointerException
        at MyUtils.bubblesort(Book2.java:222)
        at TestBook.main(Book2.java:451)

error in bubblesort
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 400 total points
Comment Utility
I'd have coded to keep convention with how arrays are indexed, and start you loop at zero, and go on to length -1. If you do this, you will need to adjust your inner loops, but it will make it easier for other experts to help you if you stick to convention.
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
The code in english

import java.io.*;
import java.util.*;
import java.lang.*;



class UserInput {

public static String getString()
{
String line;
InputStreamReader eisodosString=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosString);
try {
line = br.readLine();
return line; 
}
catch (Exception e) {
return "Lathos" ; 
}
}

public static int getInteger() {
String line;
InputStreamReader eisodosInt=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosInt);
try{
line=br.readLine();
int i=Integer.parseInt(line);
return i;
}
catch(Exception e) {
return -1;
}
}

public static double getDouble() {
String line;
InputStreamReader eisodosDouble=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosDouble);
try{
line=br.readLine();
double i=Double.parseDouble(line);
return i;
}
catch(Exception e) {
return -1;
}
}

public static float getFloat() {

String line;
InputStreamReader eisodosFloat=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(eisodosFloat);
try{
line=br.readLine();
float i=Float.parseFloat(line);
return i;
}
catch(Exception e) {
return -1;
}
}
}



class Book {
	private String tilte;  // dhlwsh metablitwn ws private
	private String name;
	private String ISBN;
	private String Publishing_House;
	private int pages;
	private int year;
	private double price;
	Book(String x,String y,String z,String a,int b,int c,double d){  //dhmiourgia domhth gia dhlwsh metablhtwn
		tilte=x;
		name=y;
		ISBN=z;
		Publishing_House=a;
		pages=b;
		year=c;
		price=d;
		
	}
	public void settilte(String tiltevi) {  // edw dhlwnontai oi set kai oi get methodoi
		tilte=tiltevi;
		}
		
	public String get_tilte() {
		return tilte;
	}
	
	public void setname(String namesyg) {
		name=namesyg;
		}

	public String get_name() {
		return name;
	}
	public void setISBN(String ISBN2) {
		ISBN=ISBN2;
		}

	public  String getISBN(){
		return ISBN;
	}
	public void setPublishing_House(String EPublishing_House) {
		Publishing_House=EPublishing_House;
		}

	public String get_Publishing_House() {
		return Publishing_House;
	}
	public void setpages(int Apages) {
		pages=Apages;
		}

	public int get_pages() {
		return pages;
	}
	public void setyear(int Ekyear) {
		year=Ekyear;
		}

	public int get_year(){
		return year;
	}
	public void setprice(double price2) {
		price=price2;
		}

	public double get_price(){
		return price;
	}
	
        @Override
        public String toString()   //dhmiourgia methodou toString gia tn emfanish tn parapanw parametrwn
   {
       return tilte+"     "+name+"     "+ISBN+"    "+Publishing_House+"    "
               +pages+"     "+year+"     "+price+"     "; 
   }
}


class MyUtils {
	public static int seqSearch(Book [] books,int key){  //methodos seiriakhs anazitisis
		for(int i=0;i<books.length;i++)
			if(books[i].get_year()==key)
				return i;
		return -1;
	}
	public static int seqSearch(Book [] books,String key){
		for(int i=0;i<books.length;i++)
			if(key.equals(books[i].getISBN())) {
                    return i;
                } else {
        }
		return -1;
     }
	public static int search(Book [] books,int key){
		int left=0,right=books.length-1;
		return binSearch(books,key,left,right);
	}
	public static int binSearch(Book [] books,int key,int left,int right){  //methodos duadikhs anazitisis
		int mid,pos=-1;
		while(pos==-1 && left<=right){
			mid=(left+right)/2;
			if(key<books[mid].get_year())
				right=mid-1;
			else
				if(key>books[mid].get_year())
					left=mid+1;
				else
					pos=mid;
		}
		return pos;
		}
	public static int search(Book [] books,String key){
		int left=0,right=books.length-1;
		return binSearch(books,key,left,right);
	}
	public static int binSearch(Book [] books,String key,int left,int right){
		int mid,pos=-1;
		while(pos==-1 && left<=right){
			mid=(left+right)/2;
			if(key.compareTo(books[mid].getISBN())<0)
				right=mid-1;
			else
				if(key.compareTo(books[mid].get_Publishing_House())>0)
					left=mid+1;
				else
					pos=mid;
		}
		return pos;
	}


	//arxh anazitisis value metaksi dio timwn
    public static void valueSearch(Book[] books, double start, double end) {
 System.out.println();
        System.out.println("Book name" + "    " + "Author" + "          " + " ISBN" + "    " + " Publishing_House" + "  Pages" + "  " + " Year" + "  " + "   Price");
 System.out.println();
 for (int i = 0; i < books.length; i++) {
           if (books[i] != null) { 
   if (books[i].get_price() >= start && books[i].get_price() <= end) {
                 System.out.println(books[i]);
   System.out.println();
            } else {
                 System.out.println("the Book in the cell " + i + " is outside target range ");
   System.out.println();
            } //end else
        } //end if arxiki
    }//end for
}//end anazitisis value


  public static void bubblesort (Book[] books) {
 		int i, j ;
	 		double temp;
                        String key;
		 boolean flag=true;
 		for (i=1; i < books.length; i++) {
  			flag=true;
  		for (j=0; j < books.length-i; j++) {
    		if (books[j].get_price() > books[j+1].get_price()) { 
    		temp=books[j].get_price();
     		books[j].setprice(books[j+1].get_price());
     		books[j+1].setprice(temp);
     		flag=false;
   		 
	
                 if (books[j].get_year() > books[j+1].get_year()) { 
    		temp=books[j].get_year();
     		books[j].setyear(books[j+1].get_year());
     		books[j+1].setyear((int) temp);
     		flag=false;
   		 
                 }
                }
                
                
   		if (flag) {
                 }
 		
                }
}
                }
                
        
              public static void inssort (Book[] books) {
		for(int i=1; i < books.length; i++) {
		int key = books[i].get_year();
		int position=i;
		// shift larger values to the right
		while(position>0 && books[position-1].get_year() > key) {
		books[position].setyear (books[position-1].get_year());
		position--;
		}
		books[position].setyear ((int) key);
                
		
              
                
		while(position>0 && books[position-1].get_price() > key) {
		books[position].setprice (books[position-1].get_price());
		position--;
		}
		books[position].setprice ((int) key);
              
                }
              }
              
              public static void selsort (Book[] books) {
			int min, temp;
			for(int i = 0; i < books.length-1; i++){
			min=i;
			for(int scan=i+1; scan <books.length; scan++)
			if(books[scan].get_year() < books[min].get_year())
			min=scan;
			//swap the values
			temp = books[min].get_year();
			books[min].setyear(books[i].get_year());
			books[i].setyear ((int) temp);
			}
		} // method sort
		} //class Selection_Sort
              
              



class TestBook {
	static int position=0;
	public static void main(String[] args)
	throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		Book [] books=new Book[10];
		String ISBN2 =null;
		int choice=0;
		int choice2;
		int choice3;
		int choice4;
		int choice5;
		int i=0;
 		int YearSearch;
		menou:
		while(choice!=5){
		System.out.println("Selection list: ");
		System.out.println("1.\tImport data book");
		System.out.println("2.\tBook search");
		System.out.println("3.\tView books price within limits");
		System.out.println("4.\tClassification books");
                System.out.println("5.\tShow all items of books");
		System.out.println("6.\tEnd");
		System.out.println();
		do{
		System.out.println("Give selection(1-4): ");
		choice=UserInput.getInteger();
		}while(choice<1 && choice>5);
		switch(choice){
		case 1:  //dhmiourgia prwtis choices
			boolean gem=checkArray(books,position);
			if(gem) break;
			boolean eis=true;
			if(i<books.length && eis==true){
			System.out.print("Give the book's title: ");
			String tilte=UserInput.getString();
			System.out.print("Give the author's name: ");
			String name_s=UserInput.getString();
			System.out.print("Give the Publishing House: ");
			String Publishing_House=UserInput.getString();
			System.out.print("Give the pages number: ");
			int pages=UserInput.getInteger();
			System.out.print("Give the year of publication: ");
			int year=UserInput.getInteger();
			System.out.print("Give the price: ");
			double price=UserInput.getDouble();
			books[position]=new Book(tilte,name_s,getISBN(),Publishing_House,pages,year,price);
			eis=false;
			i++;
			break;
			}
		//}
		 case 2:  //dhmiourgia deuteris epilogis
   				System.out.println("\tField selection search");
  			 System.out.println("1.ISBN");
   				System.out.println("2.Release year");
   				System.out.println("3.Back to the first selection list");
   				System.out.println();
   				do{  //eksasfalisi oti tha dwthei epilogi apo 1-5
   				System.out.println("Give selection(1-3): ");
   			choice2=UserInput.getInteger();
   			}while(choice2<1 && choice2>3);
   			if(choice2==1 || choice2==2){
   			 System.out.println("\tSelection search method");
    				System.out.println("1.Sequence search");
    			System.out.println("2.Binary search");
    				System.out.println("3.Back to the field selection search");
    				System.out.println();
   				 do{  // eksasfalisi oti tha dwthei epilogi apo 1-3
   				 System.out.println("Give selection(1-3)");
    			choice3=UserInput.getInteger();
     			  }while(choice3>1 && choice3<3);
   			 for( i=0;i<books.length;i++){
  			  if(books[i]==null){
    		 System.out.println("The table is not empty");
   		  break;
   				 }
  	 		 }
   			 if(choice2==1 && choice3==1){
    				 System.out.println("Issued the isbn to do  the search ");
    			 ISBN2 = getISBN();
    				 System.out.println(MyUtils.seqSearch(books,ISBN2));
    			}
    			 else if(choice2==1 && choice3==2){
     			 System.out.println("Issued the isbn to do  the search ");
     				 ISBN2 = getISBN();
     				 System.out.println(MyUtils.search(books,ISBN2));
   			 }
    		 		else if(choice2==1 && choice3==3)
     			 continue menou;
     
    			 else if(choice2==2 && choice3==1){
    			 System.out.println("Issued the year to do  the search ");
     			 YearSearch = UserInput.getInteger();
     			 System.out.println(MyUtils.seqSearch(books,YearSearch));
    			 }
    			 else if(choice2==2 && choice3==2){
     			 System.out.println("Issued the year to do  the search ");
     			 YearSearch = UserInput.getInteger();
     			 System.out.println(MyUtils.search(books,YearSearch));
    				 }
    			 else if(choice2==2 && choice3==3){
     			 continue menou;
     			}
      
    			 break;
   			}
		case 3:  // dhmiourgia choices 3
			for(i=0;i<books.length;i++){
				if(books[i]==null){
					System.out.println("The table is not empty");
					break;
				}
				}
			System.out.print("Give the limits: ");
			double p=Double.parseDouble(br.readLine());
			double a=Double.parseDouble(br.readLine());
			MyUtils.valueSearch(books,p, a);
		break;

                    
                    case 4: //dhmiourgis choices 4
			System.out.println("\tSelection sort fields");
  			 System.out.println("1.Book's price");
   				System.out.println("2.Year");
   				System.out.println("3.ISBN");
				System.out.println("4.Name");
				System.out.println("5.Back to the selection list");
   				System.out.println();
			do{  //eksasfalisi oti tha dwthei epilogi apo 1-5
   				System.out.println("Give Selection(1-5): ");
				choice4=UserInput.getInteger();
				}while(choice4<1 && choice4>5);
   			if(choice4==1 || choice4==2){
			System.out.println("\tSelection search method");
    				System.out.println("1.Bubblesort");
    				System.out.println("2.Insertion sort");
    				System.out.println("3.selection sort");
				System.out.println("4.Grigorh Taksinomhsh");
				System.out.println("5.Taksinomhsh me Sygxwneysh");
				System.out.println("6.Epistrofh sthn Epilogh Pediou Taksinomhshs");
    				System.out.println();
			do{  // eksasfalisi oti tha dwthei epilogi apo 1-5
   				 System.out.println("Give selection(1-5)");
				choice5=UserInput.getInteger();
     			  }while(choice5>1 && choice5<5);
   			 for( i=0;i<books.length;i++){
  			  if(books[i]==null){
    		 System.out.println("The table isn't full");
   		  break;
				}
			}
			if(choice4==1 && choice5==1){
    				 System.out.println("Issued the price to do  the search");
                            double price2 = UserInput.getDouble();
    				 MyUtils.bubblesort(books);
    			}
                        else if(choice4==1 && choice5==2) {
                            System.out.println("Issued the price to do  the search");
                            double price2 = UserInput.getDouble();
    				 MyUtils.inssort(books);
                        }
                        if(choice4==2 && choice5==1) {
                                System.out.println("Issued the year to do  the search");
                            int Ekyear = UserInput.getInteger();
    				 MyUtils.bubblesort(books);
 
   			}
                        
                        if(choice4==2 && choice5==2) {
                             System.out.println("Issued the year to do  the search");
                            int Ekyear = UserInput.getInteger();
    				 MyUtils.inssort(books);
                        
                 break;
                        }
                        }



		case 5:  // dhmiourgia choices 4
			for( i=0;i<books.length;i++)
				System.out.println(books[i]);
		break;
		}
	}
	}
	public static boolean checkArray(Book books[],int position){
		boolean checks=false;
		if(position>=10){
			checks=true;
		}
		return checks;
	}
	public static boolean checkISBN(String ISBN1){
			if(ISBN1.length()==10){
				char p1=ISBN1.charAt(0);
				int ap=p1*10;
				char p2=ISBN1.charAt(1);
				int ap2=p2*9;
				char p3=ISBN1.charAt(2);
				int ap3=p3*8;
				char p4=ISBN1.charAt(3);
				int ap4=p4*7;
				char p5=ISBN1.charAt(4);
				int ap5=p5*6;
				char p6=ISBN1.charAt(5);
				int ap6=p6*5;
				char p7=ISBN1.charAt(6);
				int ap7=p7*4;
				char p8=ISBN1.charAt(7);
				int ap8=p8*3;
				char p9=ISBN1.charAt(8);
				int ap9=p9*2;
				char p10=ISBN1.charAt(9);
				int ap10=p10*1;
				int athroisma=ap+ap2+ap3+ap4+ap5+ap6+ap7+ap8+ap9+ap10;
				if(athroisma%11==0)
					return true;
			}
			else if(ISBN1.length()==13){
				char ps1=ISBN1.charAt(0);
				int apot=ps1*1;
				char ps2=ISBN1.charAt(1);
				int apot2=ps2*3;
				char ps3=ISBN1.charAt(2);
				int apot3=ps3*1;
				char ps4=ISBN1.charAt(3);
				int apot4=ps4*3;
				char ps5=ISBN1.charAt(4);
				int apot5=ps5*1;
				char ps6=ISBN1.charAt(5);
				int apot6=ps6*3;
				char ps7=ISBN1.charAt(6);
				int apot7=ps7*1;
				char ps8=ISBN1.charAt(7);
				int apot8=ps8*3;
				char ps9=ISBN1.charAt(8);
				int apot9=ps9*1;
				char ps10=ISBN1.charAt(9);
				int apot10=ps10*3;
				char ps11=ISBN1.charAt(10);
				int apot11=ps11*1;
				char ps12=ISBN1.charAt(11);
				int apot12=ps12*3;
				char ps13=ISBN1.charAt(12);
				int apot13=ps13*1;
				int athroisma1=apot+apot2+apot3+apot4+apot5+apot6+apot7+apot8+apot9+apot10+apot11+apot12+apot13;
				if(athroisma1%10==0)
					return true;
			else
				return false;	
		}
		return false;
	}
         public static String getISBN()   //method to get ISBN
 		throws IOException{
 		 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 		 String ISBN1 = " " ;
  			boolean ele;
  			do{
      System.out.print("Give the ISBN : ");
      ISBN1=UserInput.getString();
      ele=checkISBN(ISBN1);
      }while(ele!=true);
  return ISBN1;
  
 }
}

Open in new window


and the error:
        Selection search method
1.Bubblesort
2.Insertion sort
3.selection sort
4.Grigorh Taksinomhsh
5.Taksinomhsh me Sygxwneysh
6.Epistrofh sthn Epilogh Pediou Taksinomhshs

Give selection(1-5)
1
The table isn't full
Issued the price to do  the search
25
Exception in thread "main" java.lang.NullPointerException
        at MyUtils.bubblesort(Book3.java:227)
        at TestBook.main(Book3.java:449)
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
I'm sorry but i can't understand. Could you give me more details please?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
Comment Utility
That compilation unit incidentally should be TestBook.java and contain the declaration
public class TestBook

Open in new window

since it's the only class to have a main method.

Please attach the data file too so books can be loaded
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:Panos
Comment Utility
Hi CEHJ

When you say "declaration" what do you mean?
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
I think that in case 4 didn't need System.out.println in line 447,452,457,464

if(choice4==1 && choice5==1){
                            
    			double price2 = UserInput.getDouble();
    				 MyUtils.bubblesort(books);
    			}
                        else if(choice4==1 && choice5==2) {
                            
                            double price2 = UserInput.getDouble();
    				 MyUtils.inssort(books);
                        }
                        if(choice4==2 && choice5==1) {
                                
                            int Ekyear = UserInput.getInteger();
    				 MyUtils.bubblesort(books);
 
   			}
                        
                        if(choice4==2 && choice5==2) {
                             
                            int Ekyear = UserInput.getInteger();
    				 MyUtils.inssort(books);

Open in new window

0
 
LVL 2

Author Comment

by:Panos
Comment Utility
CEHJ could you show me a tutorial to understand what did you say please ?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
When you say "declaration" what do you mean?
I mean it should contain the code i posted instead of what's already there. You didn't post the data file btw
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 400 total points
Comment Utility
public static void BubbleSorter( Book [ ] books ){

     int j;
     boolean sorted = false;  
     Book temp;   

     while (!sorted){
            
            sorted = true;

            for( j=0;  j < books.length -1;  j++ ){

                   if ( books[ j ] > books[j+1] ){

                           temp = books[ j ]; 
                           books[ j ] = books[ j+1 ];
                           books[ j+1 ] = temp;
                           sorted = false;  

                  } 

            } 

      } 

} 

Open in new window


If you insert the other swap you want to do into this skeleton, it should work. (BTW I am sure you realise that your second Book member swap is conditional on the first being swappable). (And of course you still need to add the lookup methods into each book for the var gets).

(And it's better to make your main loop controller a 'while' rather than 'for' loop).
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
Thank you very much it is a good idea and it works. I decided to make my code with switch case.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
it works. I decided to make my code with switch case.
??
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
>>switch case.<<

Oh right. Do you mean for the bubble sort? How have you done that?
0
 
LVL 2

Author Comment

by:Panos
Comment Utility
static void bubbleSort(Book[] books, int epilogh, int counter){
       
       
       
       switch(epilogh){
           
           case 1:
               int i,j;       
       boolean flag;
       double temp;
               String key;
               
               for(i=1; i<counter; i++){
                   flag=true;
                   for(j=0; j<counter-i; j++){
                       if((books[i].get_onoma().compareTo(books[j+1].get_onoma()))> 0){
                          key=books[j].get_onoma();
     		books[j].setonoma(books[j+1].get_onoma());
     		books[j+1].setonoma( key);                           
                           flag=false;
                       }                           
                   }
                   if(flag)return;
               }
               break;
               
           case 2:
               
               for(i=1; i<counter; i++){
                   flag=true;
                   for(j=0; j<counter-i; j++){
                       if (Long.parseLong(books[j].getISBN()) > Long.parseLong(books[j+1].getISBN())){
                           key=books[j].getISBN();
     		books[j].setISBN(books[j+1].getISBN());
     		books[j+1].setISBN( key);                        
                           flag=false;
                       }                           
                   }
                   if(flag)return;
               }
               break;
               
           case 3:
               
               for(i=1; i<counter; i++){
                   flag=true;
                   for(j=0; j<counter-i; j++){
                       if (books[j].get_etos() > books[j+1].get_etos()){
                           temp=books[j].get_etos();
     		books[j].setetos(books[j+1].get_etos());
     		books[j+1].setetos((int) temp);                         
                           flag=false;
                       }                           
                   }
                   if(flag)return;
               }
               break;
               
           case 4:    
               
               for(i=1; i<counter; i++){
                   flag=true;
                   for(j=0; j<counter-i; j++){
                       if (books[j].get_timh() > books[j+1].get_timh()){
                           temp=books[j].get_timh();
            books[j].settimh(books[j+1].get_timh());
     		books[j+1].settimh(temp);
                           flag=false;
                       }                           
                   }
                   if(flag)return;
               }
               break;
           
       }
    
}
}

Open in new window


I think so is made  for the insertion sort too.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now