Please open the menu to show more

Thursday, July 27, 2017

The best solution for the assignment given to batch 2 to 4 on 26, JULY, 2017, read data from file using scanner class

This wonderfull code is implemented by Pankaj Kumar Gupta, Branch ECE, NIT, Arunachal Pradesh

In this code we will fetch the data from the file using scanner class.

The following methods of scanner class will be used in this class =>
1. String next(), to get a text without spaces
2. boolean hasNext(), to check if a data is available for input
3. boolean hasNextInt(), to check if data is of type int or not
4. int nextInt(), to get an int value

Note: Before using this code please create a text file named as books.txt inside c: drive and store some data inside that file.
The format of data should be follwoing

TOPIC<space>AUTHOR<SPACE>PRICE<SPACE>PUBLISHER

FOR EXAMPLE =>

c ramesh 230 bp
java david 340 

------------------------------------------------------------------------------------

## Please save this class inside BookStore.java file ##


import java.io.*;
import java.util.*;
public class BookStore {
 public static void main(String[] arg){
  try{
 FileInputStream fis=new FileInputStream("c:books.txt");
 Scanner sc=new Scanner(fis);
 Scanner sc1=new Scanner(System.in);
 System.out.println("Find a Book");
 System.out.println("Press 1 by topic");
 System.out.println("Press 2 by author");
 System.out.println("Press 3 by price range");
 System.out.println("Press 4 by publication");
 System.out.println("Press 5 by topic and author");
 System.out.println("Press 6 by topic and price range");
 System.out.println("Press 7 by topic and publication");
 System.out.println("Press 8 by Author and price");
 System.out.println("Press 9 by Author and publication");
 System.out.println("Press 10 by price and publication");
 System.out.println("Press 11 by Topic, Author and price");
 System.out.println("Press 12 by Topic, Author and publication");
 System.out.println("Press 13 by Topic, price and publication");
 System.out.println("Press 14 by Author, price and publication");
 System.out.println("Press 15 by Topic, Author, price and publication");
 
 int choice=sc1.nextInt();
 String[] str={" "," "," "," "};
 int j=1;
switch(choice){
case 1 :
    System.out.print("Enter book topic\t");
String topic=sc1.next();
 
 
while(sc.hasNext()){
for(int i=0;i<4;i++){
if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(topic.equalsIgnoreCase(str[0]))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
sc.nextLine();
   }
break;
case 2 :
                 System.out.print("Enter book Author\t");
String author=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(author.equalsIgnoreCase(str[1]))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 3 :
                 System.out.println("Enter book price range from low to high eg.[100  200]\t");
System.out.println("Enter lowest price");
float price1=sc1.nextFloat();
System.out.println("Enter highest price");
float price2=sc1.nextFloat();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if((Float.parseFloat(str[2])>=price1) && (Float.parseFloat(str[2])<=price2))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();
   
   }
break;
            case 4 :
                 System.out.print("Enter book publication\t");
String publication=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(publication.equalsIgnoreCase(str[3]))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
            case 5 :
                 System.out.print("Enter book title\t");
String title1=sc1.next();
System.out.print("Enter book Author\t");
String author1=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(title1.equalsIgnoreCase(str[0]) && author1.equalsIgnoreCase(str[1]))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 6 :
                 System.out.print("Enter book title\t");
String title2=sc1.next();
System.out.println("Enter lowest price");
float priceA=sc1.nextFloat();
System.out.println("Enter highest price");
float priceB=sc1.nextFloat();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(title2.equalsIgnoreCase(str[0]) && ((Float.parseFloat(str[2])>= priceA) && (Float.parseFloat(str[2])<=priceB)))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
             case 7 :
                 System.out.print("Enter book title\t");
String title3=sc1.next();
System.out.print("Enter book publication\t");
String publicationA=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(title3.equalsIgnoreCase(str[0]) && (publicationA.equalsIgnoreCase(str[3])))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 8 :
                 System.out.print("Enter book Author\t");
String author2=sc1.next();
System.out.println("Enter lowest price");
float priceC=sc1.nextFloat();
System.out.println("Enter highest price");
float priceD=sc1.nextFloat();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(author2.equalsIgnoreCase(str[1]) && ((Float.parseFloat(str[2])>=priceC) && (Float.parseFloat(str[2])<=priceD)))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 9 :
                 System.out.print("Enter book Author\t");
String author3=sc1.next();
System.out.print("Enter book publication\t");
String publicationB=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(author3.equalsIgnoreCase(str[1]) && (publicationB.equalsIgnoreCase(str[3])))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 10 :
                 System.out.println("Enter lowest price");
float priceE=sc1.nextFloat();
System.out.println("Enter highest price");
float priceF=sc1.nextFloat();
System.out.print("Enter book publication\t");
String publicationC=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(((Float.parseFloat(str[2])>=priceE) && (Float.parseFloat(str[2])<=priceF)) && (publicationC.equalsIgnoreCase(str[3])))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 11 :
                 System.out.print("Enter book title\t");
String title4=sc1.next();
System.out.print("Enter book Author\t");
String author4=sc1.next();
System.out.println("Enter lowest price");
float priceG=sc1.nextFloat();
System.out.println("Enter highest price");
float priceH=sc1.nextFloat();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(title4.equalsIgnoreCase(str[0]) && (author4.equalsIgnoreCase(str[1])) && ((Float.parseFloat(str[2])>= priceG) && (Float.parseFloat(str[2])<=priceH)))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 12 :
                 System.out.print("Enter book title\t");
String title5=sc1.next();
System.out.print("Enter book Author\t");
String author5=sc1.next();
System.out.print("Enter book publication\t");
String publicationD=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(title5.equalsIgnoreCase(str[0]) && (author5.equalsIgnoreCase(str[1])) && ((publicationD.equalsIgnoreCase(str[3]))))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 13 :
                 System.out.print("Enter book title\t");
String title6=sc1.next();
System.out.println("Enter lowest price");
float priceI=sc1.nextFloat();
System.out.println("Enter highest price");
float priceJ=sc1.nextFloat();
System.out.print("Enter book publication\t");
String publicationE=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(title6.equalsIgnoreCase(str[0]) && ((publicationE.equalsIgnoreCase(str[3]))) && ((Float.parseFloat(str[2])>= priceI) && (Float.parseFloat(str[2])<=priceJ)))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 14 :
                 System.out.print("Enter book author\t");
String author6=sc1.next();
System.out.println("Enter lowest price");
float priceK=sc1.nextFloat();
System.out.println("Enter highest price");
float priceL=sc1.nextFloat();
System.out.print("Enter book publication\t");
String publicationF=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if(author6.equalsIgnoreCase(str[1]) && ((publicationF.equalsIgnoreCase(str[3]))) && ((Float.parseFloat(str[2])>= priceK) && (Float.parseFloat(str[2])<=priceL)))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
case 15 :
    System.out.print("Enter book title\t");
String title7=sc1.next();
                 System.out.print("Enter book author\t");
String author7=sc1.next();
System.out.println("Enter lowest price");
float priceM=sc1.nextFloat();
System.out.println("Enter highest price");
float priceN=sc1.nextFloat();
System.out.print("Enter book publication\t");
String publicationG=sc1.next();
while(sc.hasNext()){
for(int i=0;i<4;i++){
 if(i==2)
str[i]=String.valueOf(sc.nextFloat());
else
str[i]=sc.next();
}
if((title7.equalsIgnoreCase(str[0])) && author7.equalsIgnoreCase(str[1]) && ((publicationG.equalsIgnoreCase(str[3]))) && ((Float.parseFloat(str[2])>= priceM) && (Float.parseFloat(str[2])<=priceN)))
System.out.println(j++ +" topic= "+str[0]+" Author= "+str[1]+" price= "+str[2]+" Publication= "+str[3]);
else
 sc.nextLine();  
   }
break;
 
 

 
  }catch(Exception e){ System.out.println("Thank you");}

 }

}

No comments:

Post a Comment