- 5th Aug 2022
- 03:21 am
- Admin
The program is designed to read data from a file, validate its format, and extract specific book information from it, printing the extracted information to the console. It handles potential exceptions and provides user-friendly feedback on the data validity.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Stefni {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
String fileName;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the file name: ");
fileName = scan.nextLine();
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(fileName));
String contentLine;
while ((contentLine = br.readLine())!= null)
{
String arr[] = contentLine.split(" - ");
if(arr.length<6)
{
int delimeterCount =0;
for(int i=0;i {
if(contentLine.charAt(i) == '-')
{
delimeterCount++;
}
}
if(delimeterCount != 5)
{
System.out.println("The field delimeter is missing");
}
else
{
System.out.println("Data is missing from file");
}
}
else if(arr[0].matches(""))
{
System.out.println("Book title may be missing");
}
else if(arr[1].matches(""))
{
System.out.println("Book author may be missing ");
}
else if(arr[2].matches(""))
{
System.out.println("Book publisher may be missing");
}
else if(!arr[3].matches("[-+]?[0-9]*\\.?[0-9]+"))
{
System.out.println("Book Price is not a numeric value");
}
else if (!arr[4].matches("[0-9]+"))
{
System.out.println("Number of pages is not a numeric value");
}
else if(!arr[5].matches("[0-9]+"))
{
System.out.println("ISBN is not a numeric value");
}
else
{
System.out.println("Title: "+arr[0]);
System.out.println("Author: "+arr[1]);
System.out.println("Publisher: "+arr[2]);
System.out.println("Price: "+arr[3]);
System.out.println("Pages: "+arr[4]+" pages");
System.out.println("ISBN: "+arr[5]);
System.out.println("--------------------------------------------------------");
System.out.println("");
}
}
}
catch (IOException ioe)
{
System.out.println("File not found");
}
}
}