Guest User

Untitled

a guest
Nov 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. public static void main(String[] args) throws FileNotFoundException {
  2. File file = new File("ValidInvalid.txt");
  3. String line;
  4. String[] Title = new String[100];
  5. String[] Seller = new String[100];
  6. String[] Writer = new String[100];
  7. int[] Price = new int[100];
  8. int[] BarcodeNum = new int[100];
  9.  
  10. int counter = 0;
  11. if (!file.exists()) {
  12. System.out.print("File not found.");
  13. System.exit(0);
  14. }
  15. try {
  16. Scanner read = new Scanner(file);
  17. while (read.hasNextLine()) {
  18. line = read.nextLine();
  19. String[] words = line.split(" - ");
  20. Title[counter] = words[0];
  21. Writer[counter] = words[1];
  22. Price[counter] = Integer.parseInt(words[2]);
  23. Seller[counter] = words[3];
  24. BarcodeNum[counter] = Integer.parseInt(words[4]);
  25. System.out.println(Title[counter] + Writer[counter] + Price[counter] + Seller[counter] + BarcodeNum[counter]);
  26. counter++;
  27. }
  28. read.close();
  29. } catch (NumberFormatException error) {
  30. }
  31. }
  32.  
  33. while(read.hasNextLine())
  34. {
  35. line = read.nextLine();
  36. String[] words = line.split("-");
  37. if(words.length == 5)
  38. {
  39. Title[counter] = words[0].matches("\s+") ? "No Title" : words[0];
  40. Author[counter] = words[1].matches("\s+") ? "No Author" : words[1];
  41. Price[counter] = !words[2].matches("\d+") ? 0 : Integer.parseInt(words[2]);
  42. Publisher[counter] = words[3].matches("\s+") ? "No Publisher" : words[3];
  43. ISBN[counter] = !words[4].matches("\d+") ? 0 : Integer.parseInt(words[4]);
  44. System.out.println(Title[counter] + Author[counter] + Price[counter] + Publisher[counter] + ISBN[counter]);
  45. counter++;
  46. }
  47. else
  48. {
  49. System.out.println("Invalid Data format: " + line);
  50. }
  51. }
Add Comment
Please, Sign In to add comment