Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- public class main
- {
- public static void main(String[] args)
- {
- String text;
- String filename;
- String[] splitText = new String[6];
- Float price;
- int pages;
- int ISBN;
- Scanner keyboard = new Scanner (System.in);
- System.out.println("Please enter the data file name:");
- filename = keyboard.nextLine();
- try
- {
- File Fileobject = new File (filename);
- Scanner fileReader = new Scanner (Fileobject);
- while(fileReader.hasNext())
- {
- text = fileReader.nextLine();//This reads one line of data
- splitText = text.split("-");//This seperates the data
- // remove spaces
- splitText[0] = splitText[0].trim();
- splitText[1] = splitText[1].trim();
- splitText[2] = splitText[2].trim();
- splitText[3] = splitText[3].trim();
- splitText[4] = splitText[4].trim();
- splitText[5] = splitText[5].trim();
- price = Float.parseFloat(splitText[3]);
- pages = Integer.parseInt(splitText[4]);
- ISBN = Integer.parseInt(splitText[5]);
- System.out.println(String.format("%-20s", "Title:" + splitText[0]));
- System.out.println(String.format("%-20s", "Author: " + splitText[1]));
- System.out.println(String.format("%-20s", "Publisher:" + splitText[2]));
- System.out.println(String.format("%-20s", "Price:" + "�" + splitText[3]));
- System.out.println(String.format("%-20s", "Number of pages:" + splitText[4]));
- System.out.println(String.format("%-20s", "ISBN:" + splitText[5]));
- if(splitText[0].isEmpty())
- {
- System.out.println("The book Title may be missing.");
- }
- if(splitText[1].isEmpty())
- {
- System.out.println("The book Author may be missing.");
- }
- if(splitText[2].isEmpty())
- {
- System.out.println("The book Publisher may be missing.");
- }
- if(splitText[3].isEmpty())
- {
- System.out.println("The book Price may be missing.");
- }
- try
- {
- Float.parseFloat(splitText[3]);
- }
- catch(NumberFormatException e)
- {
- System.out.println(splitText[3] + " - Book price may not be a numeric value.");
- }
- if(splitText[4].isEmpty())
- {
- System.out.println("The number of pages may be missing.");
- }
- try
- {
- Integer.parseInt(splitText[4]);
- }
- catch(NumberFormatException f)
- {
- System.out.println(splitText[4] + " - Book number of pages may not be a numeric value.");
- }
- if(splitText[5].isEmpty())
- {
- System.out.println("The book ISBN may be missing.");
- }
- try
- {
- Integer.parseInt(splitText[5]);
- }
- catch(NumberFormatException a)
- {
- throw new RuntimeException();
- //System.out.println(splitText[5] + " - Book price may not be a numeric value.");
- }
- System.out.println("---------------------------------------");
- }//End of While
- fileReader.close();
- }//End of try
- catch(FileNotFoundException e)
- {
- System.out.println("File does not exist");
- }
- //catch(ArrayIndexOutOfBoundsException e)
- //{
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement