Solved

Error in seqsearch

Posted on 2013-05-18
12
210 Views
Last Modified: 2013-05-25
Hello experts

I have a problem in my program, doesnt work the seqsearch.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()==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


  }



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:  // 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


The program is running correct but when i ask to do seqsearch it shows me this error:

Exception in thread "main" java.lang.NullPointerException
        at MyUtils.seqSearch(Book2.java:153)
        at TestBook.main(Book2.java:299)
0
Comment
Question by:Panos
  • 9
  • 3
12 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 39176977
afaics you only ever assign an instance to the first element of array 'books'. All the others, therefore, will be null
0
 
LVL 2

Author Comment

by:Panos
ID: 39177007
Hi CEHJ

I made a few changes to the code with the help of netbeans in the line 153:

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

Open in new window


and now when i give the isbn for example 0123456789 to work with seqsearch  it is showing me "0".Here is the result:

Issued the isbn to do  the search
Give the ISBN nubmer: 0123456789
0
0
 
LVL 2

Author Comment

by:Panos
ID: 39177011
How could i change it? I mean the "0". I would to show the ISBN where i put before eg:0123456789
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39177029
I made a few changes to the code with the help of netbeans
Please attach the new file
0
 
LVL 2

Author Comment

by:Panos
ID: 39177039
I'm sorry.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;
	}
	
        @Override
        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(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_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


  }



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:  // 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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39177050
It would be a lot easier to help were the interface not in Greek ;) Please demonstrate the menu options you use for a run that should work
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Author Comment

by:Panos
ID: 39177070
You have right :)

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


  }



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.\tShow all items of books");
		System.out.println("5.\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:  // 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 number: ");
      ISBN1=UserInput.getString();
      ele=checkISBN(ISBN1);
      }while(ele!=true);
  return ISBN1;
  
 }
}

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 39180375
Finally the result "0" is right. This should be  appear when i do the search.
0
 
LVL 2

Author Comment

by:Panos
ID: 39180581
I've requested that this question be closed as follows:

Accepted answer: 0 points for panosms's comment #a39177007
Assisted answer: 250 points for CEHJ's comment #a39176977
Assisted answer: 30 points for CEHJ's comment #a39177050

for the following reason:

I made a few changes to the code with the help of netbeans in the line 153:

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;
     }
0
 
LVL 2

Accepted Solution

by:
Panos earned 0 total points
ID: 39180573
I made a few changes to the code with the help of netbeans in the line 153:
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;
     }

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 39180582
I'm sorry. I made a mistake closing the question.
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 39196260
Thank you very much for your help
regards
panos
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

18 Experts available now in Live!

Get 1:1 Help Now