Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package com.company.books;
  2.  
  3. import java.awt.image.Raster;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.util.Scanner;
  7.  
  8. public class App {
  9.  
  10. public static void main(String[] args) throws FileNotFoundException {
  11.  
  12. String fileName = "./Books/GoodReads.txt";
  13.  
  14. if (args.length > 0) {
  15. fileName = args[0];
  16. }
  17.  
  18. Scanner scanner = new Scanner(new File(fileName));
  19.  
  20. while (scanner.hasNextLine()){
  21. readNextBook(scanner);
  22. }
  23.  
  24. }
  25.  
  26.  
  27.  
  28. private static void readNextBook(Scanner scanner) {
  29. int bookNumber = scanner.nextInt();
  30. skipLine(scanner);
  31. String title = scanner.nextLine();
  32. skipLine(scanner);
  33. String author = scanner.nextLine().substring(3);
  34.  
  35. String ratingString = scanner.nextLine();
  36.  
  37. String[] ratingData = ratingString.split(" avg rating — ");
  38.  
  39. String rating = ratingData[0];
  40. String numberOfRates = ratingData[1];
  41. skipLines(scanner, 7);
  42.  
  43. StringBuilder builder = new StringBuilder()
  44. .append(String.format("Book #: %d\n", bookNumber))
  45. .append(String.format("Title: %s\n", title))
  46. .append(String.format("Author: %s\n", author))
  47. .append(String.format("Rating: %s\n", rating))
  48. .append(String.format("Raters: %s\n", numberOfRates))
  49. ;
  50. System.out.println(builder.toString());
  51. }
  52.  
  53. static void skipLine(Scanner scanner){
  54. skipLines(scanner, 1);
  55. }
  56.  
  57. static void skipLines(Scanner scanner, int n){
  58. for (int i = 0; i < n; i++) {
  59. scanner.nextLine();
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement