Guest User

Untitled

a guest
Dec 12th, 2019
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5.  
  6. public class main
  7. {
  8. public static void main(String[] args)
  9. {
  10. String text;
  11. String filename;
  12. String[] splitText = new String[6];
  13. Float price;
  14. int pages;
  15. int ISBN;
  16.  
  17. Scanner keyboard = new Scanner (System.in);
  18. System.out.println("Please enter the data file name:");
  19.  
  20. filename = keyboard.nextLine();
  21. try
  22. {
  23. File Fileobject = new File (filename);
  24.  
  25. Scanner fileReader = new Scanner (Fileobject);
  26.  
  27.  
  28. while(fileReader.hasNext())
  29. {
  30. text = fileReader.nextLine();//This reads one line of data
  31.  
  32. splitText = text.split("-");//This seperates the data
  33.  
  34. // remove spaces
  35. splitText[0] = splitText[0].trim();
  36. splitText[1] = splitText[1].trim();
  37. splitText[2] = splitText[2].trim();
  38. splitText[3] = splitText[3].trim();
  39. splitText[4] = splitText[4].trim();
  40. splitText[5] = splitText[5].trim();
  41.  
  42. price = Float.parseFloat(splitText[3]);
  43. pages = Integer.parseInt(splitText[4]);
  44. ISBN = Integer.parseInt(splitText[5]);
  45.  
  46. System.out.println(String.format("%-20s", "Title:" + splitText[0]));
  47. System.out.println(String.format("%-20s", "Author: " + splitText[1]));
  48. System.out.println(String.format("%-20s", "Publisher:" + splitText[2]));
  49. System.out.println(String.format("%-20s", "Price:" + "�" + splitText[3]));
  50. System.out.println(String.format("%-20s", "Number of pages:" + splitText[4]));
  51. System.out.println(String.format("%-20s", "ISBN:" + splitText[5]));
  52.  
  53.  
  54. if(splitText[0].isEmpty())
  55. {
  56. System.out.println("The book Title may be missing.");
  57. }
  58.  
  59.  
  60.  
  61. if(splitText[1].isEmpty())
  62. {
  63. System.out.println("The book Author may be missing.");
  64. }
  65.  
  66.  
  67.  
  68. if(splitText[2].isEmpty())
  69. {
  70. System.out.println("The book Publisher may be missing.");
  71. }
  72.  
  73.  
  74.  
  75. if(splitText[3].isEmpty())
  76. {
  77. System.out.println("The book Price may be missing.");
  78. }
  79.  
  80. try
  81. {
  82. Float.parseFloat(splitText[3]);
  83. }
  84. catch(NumberFormatException e)
  85. {
  86. System.out.println(splitText[3] + " - Book price may not be a numeric value.");
  87. }
  88.  
  89.  
  90.  
  91. if(splitText[4].isEmpty())
  92. {
  93. System.out.println("The number of pages may be missing.");
  94. }
  95.  
  96. try
  97. {
  98. Integer.parseInt(splitText[4]);
  99. }
  100. catch(NumberFormatException f)
  101. {
  102. System.out.println(splitText[4] + " - Book number of pages may not be a numeric value.");
  103. }
  104.  
  105.  
  106.  
  107. if(splitText[5].isEmpty())
  108. {
  109. System.out.println("The book ISBN may be missing.");
  110. }
  111.  
  112. try
  113. {
  114. Integer.parseInt(splitText[5]);
  115. }
  116. catch(NumberFormatException a)
  117. {
  118. throw new RuntimeException();
  119. //System.out.println(splitText[5] + " - Book price may not be a numeric value.");
  120. }
  121.  
  122.  
  123. System.out.println("---------------------------------------");
  124.  
  125. }//End of While
  126. fileReader.close();
  127. }//End of try
  128.  
  129. catch(FileNotFoundException e)
  130. {
  131. System.out.println("File does not exist");
  132. }
  133.  
  134. //catch(ArrayIndexOutOfBoundsException e)
  135. //{
  136.  
  137. //}
  138. }
  139. }
RAW Paste Data